Last active
August 29, 2015 14:07
-
-
Save jackofseattle/7f0ef4fd50f73bfc8878 to your computer and use it in GitHub Desktop.
Basic injectable class for AngularJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* injectable | |
* | |
* Provides a constructor to move items from the $inject property to local object members. | |
* | |
* inject = ['$q', '$http'] //translates to | |
* | |
* this.$q | |
* this.$http | |
*/ | |
export class Injectable { | |
static set inject(...args) { | |
this.prototype.$inject = this.$inject = args; | |
} | |
static get inject() { | |
return this.$inject || []; | |
} | |
constructor(...args) { | |
this.inject.forEach((dep, idx) => { | |
if (args.length < idx - 1) { | |
throw new Error(`could not read ${dep} - no more arguments passed`); | |
} | |
this[dep] = args[idx]; | |
}); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment