Permalink
Browse files

feat(angular.Module): add `decorator` method

Closes #11305
Closes #11300
  • Loading branch information...
1 parent 9093efe commit e57138d7eff1210f99238c475fff57530bf0ab19 @RodneyEbanks RodneyEbanks committed with petebacondarwin Mar 13, 2015
Showing with 14 additions and 0 deletions.
  1. +12 −0 src/loader.js
  2. +2 −0 test/loaderSpec.js
View
@@ -193,6 +193,18 @@ function setupModuleLoader(window) {
*/
constant: invokeLater('$provide', 'constant', 'unshift'),
+ /**
+ * @ngdoc method
+ * @name angular.Module#decorator
+ * @module ng
+ * @param {string} The name of the service to decorate.
+ * @param {Function} This function will be invoked when the service needs to be
+ * instantiated and should return the decorated service instance.
+ * @description
+ * See {@link auto.$provide#decorator $provide.decorator()}.
+ */
+ decorator: invokeLater('$provide', 'decorator'),
+
/**
* @ngdoc method
* @name angular.Module#animation
View
@@ -32,6 +32,7 @@ describe('module loader', function() {
var myModule = window.angular.module('my', ['other'], 'config');
expect(myModule.
+ decorator('dk', 'dv').
provider('sk', 'sv').
factory('fk', 'fv').
service('a', 'aa').
@@ -46,6 +47,7 @@ describe('module loader', function() {
expect(myModule.requires).toEqual(['other']);
expect(myModule._invokeQueue).toEqual([
['$provide', 'constant', ['abc', 123]],
+ ['$provide', 'decorator', ['dk', 'dv']],
['$provide', 'provider', ['sk', 'sv']],
['$provide', 'factory', ['fk', 'fv']],
['$provide', 'service', ['a', 'aa']],

3 comments on commit e57138d

@thomaswmanion

Anyone find any good usage examples of the decorator?

@thomaswmanion

Nevermind... Pretty straight forward :)

angular.module('myApp').service('myService', function() {
  this.test = 'Hi';
});
angular.module('myApp').decorator('myService', function($delegate) {
  $delegate.test = 'bye';
  return $delegate;
});
@NicBright

I once monkey-patched Angular UI bootstraps' "Modal" service to $emit an event once the modal was opened. This way I was able to implement a listener which focused the first form control inside the modal as a convenience to our users.

Please sign in to comment.