build_your_own_angularjs_sample
build_your_own_angularjs_sample
build_your_own_angularjs_sample
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Build Your Own Angular Chapter 1. Scopes And Digest<br />
if (!self.$$areEqual(newValue, oldValue, watcher.valueEq)) {<br />
self.$$lastDirtyWatch = watcher;<br />
watcher.last = (watcher.valueEq _.cloneDeep(newValue) : newValue);<br />
watcher.listenerFn(newValue,<br />
(oldValue === initWatchVal newValue : oldValue),<br />
self);<br />
dirty = true;<br />
} else if (self.$$lastDirtyWatch === watcher) {<br />
return false;<br />
}<br />
} catch (e) {<br />
console.error(e);<br />
}<br />
});<br />
return dirty;<br />
};<br />
What remains is exception handling in $evalAsync and $$postDigest. Both are used to<br />
execute arbitrary functions in relation to the digest loop – one before a digest and one<br />
after it. In neither case do we want an exception to cause the loop to end prematurely.<br />
For $evalAsync we can define a test case that checks that a watch is run even when an<br />
exception is thr<strong>own</strong> from one of the functions scheduled for $evalAsync:<br />
test/scope_spec.js<br />
it("catches exceptions in $evalAsync", function(done) {<br />
scope.aValue = 'abc';<br />
scope.counter = 0;<br />
});<br />
scope.$watch(<br />
function(scope) { return scope.aValue; },<br />
function(newValue, oldValue, scope) {<br />
scope.counter++;<br />
}<br />
);<br />
scope.$evalAsync(function(scope) {<br />
throw "Error";<br />
});<br />
setTimeout(function() {<br />
expect(scope.counter).toBe(1);<br />
done();<br />
}, 50);<br />
40 ©2014 Tero Parviainen Errata / Submit