Skip to content

Instantly share code, notes, and snippets.

@goranprijic
Last active August 29, 2015 14:05
Show Gist options
  • Save goranprijic/9d2176ae704fd0ea4581 to your computer and use it in GitHub Desktop.
Save goranprijic/9d2176ae704fd0ea4581 to your computer and use it in GitHub Desktop.
Implementing Traits in AngularJS
angular.module('myApp')
.factory('Traits', function() {
return {
apply: function (_class, _trait) {
_.each(_trait, function (method, name) {
_class.prototype[name] = method;
});
}
}
});
angular.module('myApp')
.factory('MyTrait', function() {
return {
someMethod: function (param) {
//
},
anotherMethod: function (param) {
//
}
}
});
angular.module('myApp')
.factory('MyResource', function($resource, Traits, MyTrait) {
var resource = $resource();
Traits.apply(resource, MyTrait);
return resource;
});

Simple way how to implement Traits in AngularJS

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