Simple way how to implement Traits in AngularJS
Last active
August 29, 2015 14:05
-
-
Save goranprijic/9d2176ae704fd0ea4581 to your computer and use it in GitHub Desktop.
Implementing Traits in AngularJS
This file contains hidden or 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
| angular.module('myApp') | |
| .factory('Traits', function() { | |
| return { | |
| apply: function (_class, _trait) { | |
| _.each(_trait, function (method, name) { | |
| _class.prototype[name] = method; | |
| }); | |
| } | |
| } | |
| }); |
This file contains hidden or 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
| angular.module('myApp') | |
| .factory('MyTrait', function() { | |
| return { | |
| someMethod: function (param) { | |
| // | |
| }, | |
| anotherMethod: function (param) { | |
| // | |
| } | |
| } | |
| }); |
This file contains hidden or 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
| angular.module('myApp') | |
| .factory('MyResource', function($resource, Traits, MyTrait) { | |
| var resource = $resource(); | |
| Traits.apply(resource, MyTrait); | |
| return resource; | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment