580 likes | 709 Views
What’s the $scope? . Part II | 3 June 2013. Agenda. Overview How to watch; how watching is implemented Scope hierarchy Advanced Use Cases Events $apply, $ eval , $destroy Best Practices & Tricks. Opower today. A SaaS Customer Engagement Platform. The Company
E N D
What’s the $scope? • Part II | 3 June 2013
Agenda • Overview • How to watch; how watching is implemented • Scope hierarchy • Advanced Use Cases • Events • $apply, $eval, $destroy • Best Practices & Tricks
Opower today A SaaSCustomer Engagement Platform • The Company • Serving 85 utilities in 6 countries • Over 2TWh saved to date • 40% of US household data under management totaling 100 billion reads • 300 people in Washington, San Francisco, London, Singapore • Forbes #10 of 100 Most Promising Companies
$scope points to your data model $scope write read View (angular templates) Controllers & Directives
$watch • Watch expression will be called many times, so it should be fast / idempotent • Making the watch expression slow in one of the few ways to really screw yourself in angular
$watch expressions • You’re not limited to just naming variables • Parsed and evaluated with parse.js • Eval demo
$watch expressions • Third arg specifies referential or deep equality • fiddle
$watch expressions • Use an empty watch expression as a wildcard
$watch expressions • Coming soon: $watchCollection
Watch / digest cycle JS event (user input, http response, etc) yes Did anything change? Fire watchers No Done!
Dirty checking v. change listeners • Less NSFW than it sounds • Excellent SO answer from Misko • Angular uses dirty checking; Knockout and Backbone use change listeners • Dirty checking requires more comparisons, so the comparisons must be fast
Dirty checking v. change listeners • Less NSFW than it sounds • Excellent SO answer from Misko • Angular uses dirty checking; Knockout and Backbone use change listeners • Dirty checking requires more comparisons, so the comparisons must be fast
POJOs Backbone Angular
Excessive Changes fired • O(numTweets) change events fired with change listeners • O(1) change events fired with dirty checking
Simultaneous change listeners • Both handlers will be in progress at once with change listening • Only one handler will fire at a time with dirty checking
Watch / digest cycle example auth.loggedIn auth.loggedInCount mostRecentServerUpdate
Watch / digest cycle example • Digest iteration count: 0
Watch / digest cycle example • Digest iteration count: 1
Watch / digest cycle example • Digest iteration count: 2
Watch / digest cycle example • Digest iteration count: Done!
Watch / digest cycle example • Digest iteration count: 0
Watch / digest cycle example • Digest iteration count: 1
$scopes are arranged in a hierarchy $rootScope … trending-tweets ng-repeat-0 ng-repeat-n tweet tweet Linked list
$scopes are arranged in a hierarchy Main Content Inheritance works with controller scopes, too. Subsection
Prototypes $rootScope Child scopes prototypically inherit from their parents top-tweets
Prototypes $rootScope Child scopes prototypically inherit from their parents unless the child is an isolate scope top-tweets
Prototypes $rootScope Transcluded scopes inherit from their parents Transcluded and isolated scopes are siblings ng-transclude trending-tweets
Agenda • Overview • How to watch; how watching is implemented • Scope hierarchy • Advanced Use Cases • Events • $apply, $eval, $destroy • Best Practices & Tricks
Firing a watch/digest yourself • Want to start a cycle on scope change • Angular gives you this for free • $timeout v. setTimeout • $http • $q • Controllers, directives, etc • Start it manually to integrate with non-angular code
Firing a watch/digest yourself JS event (user input, http response, etc) yes Did anything change? Fire watchers No Done!
Firing a watch/digest yourself JS event (user input, http response, etc) yes Did anything change? Fire watchers $rootScope.$apply() No Done!
Events! $rootScope ng-repeat-0 tweet
Events! $rootScope ng-repeat-0 tweet
Events! $rootScope ng-repeat-0 tweet
$eval • Manually evaluate an expression against the scope
$destroy • Removing a DOM node doesn’t remove the associated scope • Do it yourself with $destroy • Broadcasts a $destroy event • Fiddle • ng-repeat calls $destroy: