Skip to content

Instantly share code, notes, and snippets.

@m00s
Last active August 29, 2015 14:21
Show Gist options
  • Save m00s/2f2b77960bd2f852c405 to your computer and use it in GitHub Desktop.
Save m00s/2f2b77960bd2f852c405 to your computer and use it in GitHub Desktop.
Use a provider to configure other factories

Like a Façade the ConfiguratorProvider take care of the entire app configuration.

So in your app instead of something like:

.config(function(firstProvider, secondProvider, thirdProvider){
    firstProvider.foo();
    secondProvider.bar();
    thirdProvider.baz();
});

you can have:

.config(function(configProvider){
    configProvider
      .foo()
      .bar()
      .baz();
});

You can keep all your app factories and use a single provider.

angular.module('app', [])
.factory('Factory', function () {
var foo = 'Factory (default)';
function setFoo(value) {
foo = value;
return foo;
}
function getFoo() {
return foo;
}
return {
text: getFoo,
setFoo: setFoo
}
})
.provider('Configurator', function () {
var text = 'Provider (default)';
this.setText = function(value) {
text = value;
return this;
};
this.$get = ['Factory', function(Factory) {
(function init(){
Factory.setFoo(text);
})();
return {
text: Factory.text
}
}];
})
.config(['ConfiguratorProvider', function(ConfiguratorProvider){
ConfiguratorProvider.setText('Configuration');
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment