forked from bennadel/JavaScript-Demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.htm
175 lines (125 loc) · 3.92 KB
/
index.htm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<!doctype html>
<html ng-app="Demo">
<head>
<meta charset="utf-8" />
<title>
Using $scope.$digest() As A Performance Optimization In AngularJS
</title>
<link rel="stylesheet" type="text/css" href="./demo.css"></link>
</head>
<body ng-controller="AppController">
<h1>
Using $scope.$digest() As A Performance Optimization In AngularJS
</h1>
<!--
Notice that both this P-tag and the next one both use ng-class to see CSS
classes based on the Model; however, the second P uses a child scope (created
by the bn-digest directive).
-->
<p
ng-mouseenter="setIsHot( true );"
ng-mouseleave="setIsHot( false );"
class="apply"
ng-class="{ hot: isHot }">
$apply()
</p>
<p
bn-digest
class="digest"
ng-class="{ hot: localIsHot }">
$digest()
</p>
<p class="logging">
<em>NOTE: Logging output to console.</em>
</p>
<!-- Load scripts. -->
<script type="text/javascript" src="../../vendor/jquery/jquery-2.0.3.min.js"></script>
<script type="text/javascript" src="../../vendor/angularjs/angular-1.2.min.js"></script>
<script type="text/javascript">
// Create an application module for our demo.
var app = angular.module( "Demo", [] );
// -------------------------------------------------- //
// -------------------------------------------------- //
// I control the root of the application.
app.controller(
"AppController",
function( $scope ) {
// Setting up a watcher to mimic the high number of bindings that most
// applications will have. I will be called on every digest.
$scope.$watch(
function() {
console.log( "Top-level digest 1." );
}
);
// Setting up a watcher to mimic the high number of bindings that most
// applications will have. I will be called on every digest.
$scope.$watch(
function() {
console.log( "Top-level digest 2." );
}
);
// Setting up a watcher to mimic the high number of bindings that most
// applications will have. I will be called on every digest.
$scope.$watch(
function() {
console.log( "Top-level digest 3." );
}
);
// I determine if the target element is "hot" (for display purposes).
$scope.isHot = false;
// ---
// PUBLIC METHODS.
// ---
// I set the new isHot property.
$scope.setIsHot = function( newIsHot ) {
$scope.isHot = newIsHot;
};
}
);
// -------------------------------------------------- //
// -------------------------------------------------- //
// I implement some mouse-interaction behavior without triggering digests at a
// higher level in the $scope chain.
app.directive(
"bnDigest",
function() {
// I bind the JavaScript events to the scope.
function link( $scope, element, attributes ) {
// I determine if the target element is hot.
$scope.localIsHot = false;
// I activate the element on mouse-enter.
element.mouseenter(
function() {
$scope.localIsHot = true;
// NOTE: By calling the $digest() instead of the more typical
// $apply() method, we will only trigger watchers on the local
// scope (and its children). We will NOT trigger any watchers
// on the parent scope.
$scope.$digest();
}
);
// I deactivate the element on mouse-leave.
element.mouseleave(
function() {
$scope.localIsHot = false;
// NOTE: By calling the $digest() instead of the more typical
// $apply() method, we will only trigger watchers on the local
// scope (and its children). We will NOT trigger any watchers
// on the parent scope.
$scope.$digest();
}
);
}
// NOTE: By setting scope to TRUE, the directive creates a new child scope
// that separates it from the parent scope (creating a isolated part of
// the scope chain).
return({
link: link,
restrict: "A",
scope: true
});
}
);
</script>
</body>
</html>