Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Commit 8b54f5e

Browse files
vicbmhevery
authored andcommitted
fix(ng-class): remove previously registered watch
Closes #725
1 parent 51ee329 commit 8b54f5e

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

lib/directive/ng_class.dart

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,11 @@ abstract class _NgClassBase {
150150
final NgAnimate _animate;
151151
var previousSet = [];
152152
var currentSet = [];
153+
Watch _watch;
153154

154-
155-
_NgClassBase(this.element, this.scope, this.mode, this.nodeAttrs, this._parser, this._animate)
156-
{
155+
_NgClassBase(this.element, this.scope, this.mode, this.nodeAttrs,
156+
this._parser, this._animate)
157+
{
157158
var prevClass;
158159

159160
nodeAttrs.observe('class', (String newValue) {
@@ -165,9 +166,8 @@ abstract class _NgClassBase {
165166
}
166167

167168
set valueExpression(currentExpression) {
168-
// this should be called only once, so we don't worry about cleaning up
169-
// watcher registrations.
170-
scope.watch(_parser(currentExpression, collection: true), (current, _) {
169+
if (_watch != null) _watch.remove();
170+
_watch = scope.watch(_parser(currentExpression, collection: true), (current, _) {
171171
currentSet = _flatten(current);
172172
_handleChange(scope.context[r'$index']);
173173
}, readOnly: true);
@@ -177,10 +177,8 @@ abstract class _NgClassBase {
177177
if (oldIndex == null || mod != oldIndex % 2) {
178178
if (mod == mode) {
179179
currentSet.forEach((css) => _animate.addClass(element, css));
180-
//element.classes.addAll(currentSet);
181180
} else {
182181
previousSet.forEach((css) => _animate.removeClass(element, css));
183-
//element.classes.removeAll(previousSet);
184182
}
185183
}
186184
}, readOnly: true);
@@ -191,18 +189,18 @@ abstract class _NgClassBase {
191189
if (mode == null || (index != null && index % 2 == mode)) {
192190
previousSet.forEach((css) {
193191
if (!currentSet.contains(css)) {
194-
_animate.removeClass(element, css);
192+
_animate.removeClass(element, css);
195193
} else {
196194
element.classes.remove(css);
197195
}
198196
});
199197

200198
currentSet.forEach((css) {
201199
if (!previousSet.contains(css)) {
202-
_animate.addClass(element, css);
200+
_animate.addClass(element, css);
203201
} else {
204202
element.classes.add(css);
205-
}
203+
}
206204
});
207205
}
208206

@@ -225,7 +223,7 @@ abstract class _NgClassBase {
225223
return classes.keys.where((key) => toBool(classes[key])).toList();
226224
}
227225
if (classes is String) return classes.split(' ');
228-
throw
229-
'ng-class expects expression value to be List, Map or String, got $classes';
226+
throw 'ng-class expects expression value to be List, Map or String, '
227+
'got $classes';
230228
}
231229
}

0 commit comments

Comments
 (0)