diff options
| author | Igor Minar | 2010-12-10 11:39:32 -0800 |
|---|---|---|
| committer | Igor Minar | 2010-12-10 13:22:44 -0800 |
| commit | b370fac4fc7e4c4da3b7968ed44cf3745c2ad7a4 (patch) | |
| tree | 4acd9c68b5c32904dd5b26505d329a8b1918a4fb | |
| parent | 23fc73081feb640164615930b36ef185c23a3526 (diff) | |
| download | angular.js-b370fac4fc7e4c4da3b7968ed44cf3745c2ad7a4.tar.bz2 | |
$defer service should always call $eval after callback finished
Closes #189
| -rw-r--r-- | src/services.js | 4 | ||||
| -rw-r--r-- | test/servicesSpec.js | 50 | ||||
| -rw-r--r-- | test/widgetsSpec.js | 6 |
3 files changed, 56 insertions, 4 deletions
diff --git a/src/services.js b/src/services.js index f7661310..d49b5370 100644 --- a/src/services.js +++ b/src/services.js @@ -819,12 +819,16 @@ angularServiceInject('$xhr.bulk', function($xhr, $error, $log){ * @param {function()} fn A function, who's execution should be deferred. */ angularServiceInject('$defer', function($browser, $exceptionHandler) { + var scope = this; + return function(fn) { $browser.defer(function() { try { fn(); } catch(e) { $exceptionHandler(e); + } finally { + scope.$eval(); } }); }; diff --git a/test/servicesSpec.js b/test/servicesSpec.js index 5a9a4702..bda7b47c 100644 --- a/test/servicesSpec.js +++ b/test/servicesSpec.js @@ -361,12 +361,41 @@ describe("service", function(){ it('should delegate exception to the $exceptionHandler service', function() { - $defer(function() { throw "Test Error"; }); + $defer(function() {throw "Test Error";}); expect($exceptionHandler).not.toHaveBeenCalled(); $browser.defer.flush(); expect($exceptionHandler).toHaveBeenCalledWith("Test Error"); }); + + + it('should call eval after each callback is executed', function() { + var eval = this.spyOn(scope, '$eval').andCallThrough(); + + $defer(function() {}); + expect(eval).wasNotCalled(); + + $browser.defer.flush(); + expect(eval).wasCalled(); + + eval.reset(); //reset the spy; + + $defer(function() {}); + $defer(function() {}); + $browser.defer.flush(); + expect(eval.callCount).toBe(2); + }); + + + it('should call eval even if an exception is thrown in callback', function() { + var eval = this.spyOn(scope, '$eval').andCallThrough(); + + $defer(function() {throw "Test Error"}); + expect(eval).wasNotCalled(); + + $browser.defer.flush(); + expect(eval).wasCalled(); + }); }); @@ -543,6 +572,25 @@ describe("service", function(){ $browser.defer.flush(); expect(log).toEqual('"+";"+";'); //callback has executed }); + + it('should call eval after callbacks for both cache hit and cache miss execute', function() { + var eval = this.spyOn(scope, '$eval').andCallThrough(); + + $browserXhr.expectGET('/url').respond('+'); + cache('GET', '/url', null, callback); + expect(eval).wasNotCalled(); + + $browserXhr.flush(); + expect(eval).wasCalled(); + + eval.reset(); //reset the spy + + cache('GET', '/url', null, callback); + expect(eval).wasNotCalled(); + + $browser.defer.flush(); + expect(eval).wasCalled(); + }) }); }); diff --git a/test/widgetsSpec.js b/test/widgetsSpec.js index 7253c26f..bff2c4e3 100644 --- a/test/widgetsSpec.js +++ b/test/widgetsSpec.js @@ -571,8 +571,8 @@ describe("widget", function(){ // this one should really be just '1', but due to lack of real events things are not working // properly. see discussion at: http://is.gd/ighKk - expect(element.text()).toEqual('2'); - dealoc(scope); + expect(element.text()).toEqual('4'); + dealoc(element); }); it('should evaluate onload expression when a partial is loaded', function() { @@ -587,7 +587,7 @@ describe("widget", function(){ scope.$inject('$browser').defer.flush(); expect(element.text()).toEqual('my partial'); expect(scope.loaded).toBe(true); - dealoc(scope); + dealoc(element); }); }); |
