diff options
| author | Caio Cunha | 2014-01-02 23:02:12 -0500 | 
|---|---|---|
| committer | Igor Minar | 2014-03-18 13:44:16 -0700 | 
| commit | 299b220f5e05e1d4e26bfd58d0b2fd7329ca76b1 (patch) | |
| tree | 3cfa2c8a257742ff20f24f0225a9d8abf100fdab /test/ng/compileSpec.js | |
| parent | 78057a945ef84cbb05f9417fe884cb8c28e67b44 (diff) | |
| download | angular.js-299b220f5e05e1d4e26bfd58d0b2fd7329ca76b1.tar.bz2 | |
feat($compile): add support for $observer deregistration
In order to make the behavior compatible with $rootScope.$watch and $rootScope.$on methods, and
make it possible to deregister an attribute observer, Attributes.$observe method now returns a
deregistration function instead of the observer itself.
BREAKING CHANGE: calling attr.$observe no longer returns the observer function, but a
    deregistration function instead.
    To migrate the code follow the example below:
    Before:
```
    directive('directiveName', function() {
      return {
        link: function(scope, elm, attr) {
          var observer = attr.$observe('someAttr', function(value) {
            console.log(value);
          });
        }
      };
    });
```
    After:
```
    directive('directiveName', function() {
      return {
        link: function(scope, elm, attr) {
          var observer = function(value) {
            console.log(value);
          };
          attr.$observe('someAttr', observer);
        }
      };
    });
```
Closes #5609
Diffstat (limited to 'test/ng/compileSpec.js')
| -rwxr-xr-x | test/ng/compileSpec.js | 17 | 
1 files changed, 14 insertions, 3 deletions
| diff --git a/test/ng/compileSpec.js b/test/ng/compileSpec.js index 5110c4d6..96d3c18b 100755 --- a/test/ng/compileSpec.js +++ b/test/ng/compileSpec.js @@ -1914,15 +1914,14 @@ describe('$compile', function() {    describe('interpolation', function() { -    var observeSpy, directiveAttrs; +    var observeSpy, directiveAttrs, deregisterObserver;      beforeEach(module(function() {        directive('observer', function() {          return function(scope, elm, attr) {            directiveAttrs = attr;            observeSpy = jasmine.createSpy('$observe attr'); - -          expect(attr.$observe('someAttr', observeSpy)).toBe(observeSpy); +          deregisterObserver = attr.$observe('someAttr', observeSpy);          };        });        directive('replaceSomeAttr', valueFn({ @@ -2020,6 +2019,18 @@ describe('$compile', function() {      })); +    it('should return a deregistration function while observing an attribute', inject(function($rootScope, $compile) { +      $compile('<div some-attr="{{value}}" observer></div>')($rootScope); + +      $rootScope.$apply('value = "first-value"'); +      expect(observeSpy).toHaveBeenCalledWith('first-value'); + +      deregisterObserver(); +      $rootScope.$apply('value = "new-value"'); +      expect(observeSpy).not.toHaveBeenCalledWith('new-value'); +    })); + +      it('should set interpolated attrs to initial interpolation value', inject(function($rootScope, $compile) {        $rootScope.whatever = 'test value';        $compile('<div some-attr="{{whatever}}" observer></div>')($rootScope); | 
