Share
Embed
Angular - Scope Inheritance (Digest loop part 2)
Check out part1 here:
https://speakerdeck.com/barnash/angular-digest-loop
-------------------
Based on the second chapter of "Build Your Own Angular" book:
http://teropa.info/build-your-own-angular/
And the second part of the first deck:
Making scopes inherit from one another.
barnash
May 07, 2014
Transcript
44 slides
-
How will this session help me? - Get to know
how scope inheritance works - Practice your JavaScript skills - Understand prototype inheritance better -
In the previous session - The magic behind data-binding -
An inside look into the digest loop - $digest, $watch - Basic methods of scope -
A short reminder - Scope constructor - $$areEqual - $watch
- $digest and $$digestOnce - $eval and $apply goo.gl/VWvRpS -
function Scope() { this.$$watchers = []; this.$$lastDirtyWatch = null; }
Scope.prototype.$$areEqual = function(newValue, oldValue, valueEq) { if (valueEq) { return _.isEqual(newValue, oldValue); } else { return newValue === oldValue || (typeof newValue === 'number' && typeof oldValue === 'number' && isNaN(newValue) && isNaN(oldValue)); } }; -
Scope.prototype.$watch = function(watchFn, listenerFn, valueEq) { var self = this;
var watcher = { watchFn: watchFn, listenerFn: listenerFn || function() {}, valueEq: !!valueEq }; self.$$watchers.push(watcher); return function() { var index = self. $$watchers.indexOf(watcher); if (index >= 0) { self. $$watchers.splice(index, 1); } } }; -
Scope.prototype.$$digestOnce = function() { var self = this; var dirty
= false; this.$$watchers.every(function(watch) { var newValue = watch.watchFn(self); var oldValue = watch.last; if (!self.$$areEqual(newValue, oldValue, watch.valueEq)) { watch.last = (watch.valueEq ? _.cloneDeep(newValue) : newValue); watch.listenerFn(newValue, oldValue, self); dirty = true; self.$$lastDirtyWatch = watch; } else if (self.$$lastDirtyWatch === watch) { return false; } return true; }); return dirty; }; -
Performance Watch Watch Watch Watch Watch Watch Watch Watch Watch
Watch Watch Watch Watch Watch Watch Watch -
Scope.prototype.$digest = function() { var ttl = 10; var dirty;
this.$$lastDirtyWatch = null; do { dirty = this.$$digestOnce(); if (dirty && !(ttl--)) { throw "10 digest iterations reached"; } } while (dirty); }; -
Scope.prototype.$eval = function(expr, locals) { return expr(this, locals); }; Scope.prototype.$apply
= function(expr) { try { return this.$eval(expr); } finally { this.$digest(); } }; -
Scope.$new Scope.prototype.$new = function() { var ChildScope = function() {
}; ChildScope.prototype = this; var child = new ChildScope(); return child; }; -
The meaning... var parent = new Scope(); parent.parentValue = [1,2,3];
var child = parent.$new(); console.log(child.parentValue); What will be printed? -
The meaning... var parent = new Scope(); parent.parentValue = [1,2,3];
var child = parent.$new(); console.log(child.parentValue); parent: { parentValue: [1,2,3] } child: { __proto__: parent } [1,2,3] -
The meaning… (2) var parent = new Scope(); var child
= parent.$new(); child.childValue = [1,2,3]; console.log(parent.childValue); What will be printed? -
The meaning… (2) var parent = new Scope(); var child
= parent.$new(); child.childValue = [1,2,3]; console.log(parent.childValue); parent: { } child: { __proto__: parent, childValue: [1,2,3] } undefined -
The meaning… (3) var parent = new Scope(); var child
= parent.$new(); parent.parentValue = [1,2,3]; child.parentValue.push(4); console.log(parent.parentValue); What will be printed? -
The meaning… (3) var parent = new Scope(); var child
= parent.$new(); parent.parentValue = [1,2,3]; child.parentValue.push(4); console.log(parent.parentValue); parent: { parentValue: [1,2,3] } child: { __proto__: parent } 1,2,3,4 -
The meaning… (4) var parent = new Scope(); var child
= parent.$new(); parent.name = “Ned”; child.name = “Rob”; console.log(child.name); console.log(parent.name); What will be printed? -
The meaning… (4) var parent = new Scope(); var child
= parent.$new(); parent.name = “Ned”; child.name = “Rob”; console.log(child.name); console.log(parent.name); parent: { name: “Ned” } child: { __proto__: parent, name: “Rob” } Rob, Ned -
Q - Will this code work as expected? var parent
= new Scope(); parent.value = “abc”; parent.$watch( function(scope) { return scope.value; }, function(newVal, oldVal, scope) { scope. watchCalled = true; } ); var child = parent.$new(); child.$digest(); -
Separate digests Scope.prototype.$new = function() { var ChildScope = function()
{ }; ChildScope.prototype = this; var child = new ChildScope(); child.$$watchers = []; return child; }; -
Q - Will this code work as expected? var parent
= new Scope(); var child = parent.$new(); child.val = “abc”; child.$watch( function(scope) { return scope.val; }, function(newVal, oldVal, scope) { scope. watchCalled = true; } ); parent.$digest(); -
Recursive digest function Scope() { this.$$watchers = []; this.$$lastDirtyWatch =
null; this.$$children = []; } -
Recursive digest - $new Scope.prototype.$new = function() { var ChildScope
= function() { }; ChildScope.prototype = this; var child = new ChildScope(); this.$$children.push(child); child.$$watchers = []; child.$$children = []; return child; }; -
Recursive digest - $$everyScope Scope.prototype.$$everyScope = function(fn) { if (fn(this))
{ return this.$$children.every(function(child) { return child.$$everyScope(fn); }); } else { return false; } }; -
Recursive digest - $$digestOnce Scope.prototype.$$digestOnce = function() { var self
= this; var dirty = false; var continueLoop = true; ... -
this.$$everyScope(function(scope) { scope.$$watchers.every(function(watch) { var newValue = watch.watchFn(scope); var oldValue
= watch.last; if (!scope.$$areEqual(newValue, oldValue, watch.valueEq)) { watch.last = (watch.valueEq ? _.cloneDeep(newValue) : newValue); watch.listenerFn(newValue, oldValue, scope); dirty = true; self.$$lastDirtyWatch = watch; } else if (self.$$lastDirtyWatch === watch) { continueLoop = false; return false; } return true; }); return continueLoop; }); return dirty; }; -
Watch Watch Watch Watch Watch Watch Watch Watch Watch Watch
Watch Watch Watch Watch Watch Watch Child 3 Parent Child 2 Child 1 -
$apply vs $digest - $$root function Scope() { this.$$watchers =
[]; this.$$lastDirtyWatch = null; this.$$children = []; this.$$root = this; } -
$apply vs $digest - $apply Scope.prototype.$apply = function(expr) { try
{ return this.$eval(expr); } finally { this.$$root.$digest(); } }; -
Isolated scopes Scope.prototype.$new = function(isolated) { var child; if (isolated)
{ child = new Scope(); } else { var ChildScope = function() { }; ChildScope.prototype = this; child = new ChildScope(); } this.$$children.push(child); child.$$watchers = []; child.$$children = []; return child; }; -
Isolated scopes (2) Scope.prototype.$new = function(isolated) { var child; if
(isolated) { child = new Scope(); child.$$root = this.$$root; } else { var ChildScope = function() { }; ChildScope.prototype = this; child = new ChildScope(); } this.$$children.push(child); child.$$watchers = []; child.$$children = []; return child; }; -
Destroying Scopes - $new Scope.prototype.$new = function(isolated) { var child;
if (isolated) { child = new Scope(); child.$$root = this.$$root; } else { var ChildScope = function() { }; ChildScope.prototype = this; child = new ChildScope(); } this.$$children.push(child); child.$$watchers = []; child.$$children = []; child.$parent = this; return child; }; -
Scope.prototype.$destroy = function() { if (this === this.$$root) { return;
} var siblings = this.$parent.$$children; var indexOfThis = siblings.indexOf(this); if (indexOfThis >= 0) { siblings.splice(indexOfThis, 1); } }; Destroying Scopes - $destroy -
Basically... - Scope inheritance - Recursive $digest - Scope Isolation
- Destroying scopes http://teropa.info/build-your-own-angular