Skip to content

Instantly share code, notes, and snippets.

@bclinkinbeard
Created February 22, 2014 00:44
Show Gist options
  • 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

BTW, thx for publishing this Gist.

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