Skip to content

Instantly share code, notes, and snippets.

@bclinkinbeard
Created February 22, 2014 00:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bclinkinbeard/9146766 to your computer and use it in GitHub Desktop.
Save bclinkinbeard/9146766 to your computer and use it in GitHub Desktop.
'use strict';
var _ = require('underscore');
module.exports = function () {
var proto,
injections = [];
// define constructor function
/*jshint validthis:true */
function ctor () {
var i, len, injectName, key, prop;
// assign injected dependencies as instance properties
if (ctor.$inject) {
for (i = 0, len = ctor.$inject.length; i < len; i++) {
injectName = ctor.$inject[i];
this[injectName] = arguments[i];
}
}
// automatically bind private callbacks (methods beginning with _on)
for (key in this) {
prop = this[key];
if (typeof prop === 'function' && key.indexOf('_on') === 0) {
this[key] = _.bind(this[key], this);
}
}
// call init pseudo-constructor if present
if (this.init) {
this.init.apply(this, arguments);
}
}
proto = ctor.prototype;
for (var i = 0, len = arguments.length; i < len; i++) {
// merge each set of properties into the prototype
_.extend(proto, arguments[i]);
// gather all mixin injections
if (arguments[i].$inject) {
injections = injections.concat(arguments[i].$inject);
}
}
// save injection names as a property on constructor
if (injections.length > 0) {
ctor.$inject = _.uniq(injections);
}
return ctor;
};
@ThomasBurleson
Copy link

In an alternate thread, you showed how the mixin() could be used:

'use strict';

var mix = require('sk-ng-mixin');

module.exports = mix({

    $inject: ['$scope', '$http', 'session'],

    init: function ($scope, $http, session) {
        this.initScope($scope, opts);
    },

    initScope: function ($scope, opts) {
        $scope.submit = this._onSubmit;
    },

    _onSubmit: function () {
        if (this.$scope.user.password !== this.$scope.user.verifyPassword) {
            throw new Error('Mismatched passwords!');
        }

        var config = {
            orgName: urlUtil.orgName,
            password: this.$scope.user.password,
        };

        this.$http.post(urlUtil.getResetPasswordUrl(), config)
            .then(this._onResetPasswordSuccess, this._onResetPasswordFailure);
    }
});

Thought I would copy that here for completeness.

@ThomasBurleson
Copy link

The mixin() idea is a very interesting idea... I would recommend not using an anonymous function for the init; since it unintentionally hides the purpose of the mixin class. Wonder what you think of this change:

'use strict';

var mix = require('sk-ng-mixin');

module.exports = mix({

    $inject : ['$scope', '$http', 'session'],
    init    : HomeController

});

function HomeController(urlUtil, $scope, $http, session)
{
    $scope.submit = onSubmit;

    function onSubmit() 
    {
        var user = $scope.user;

        if (user.password !== user.verifyPassword) {
            throw new Error('Mismatched passwords!');
        }

        var config = {
            orgName: urlUtil.orgName,
            password: user.password,
        };

        return $http.post( urlUtil.getResetPasswordUrl(), config)
                    .then(_onResetPasswordSuccess, _onResetPasswordFailure);
    }
}

@ThomasBurleson
Copy link

BTW, thx for publishing this Gist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment