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

Commit

Permalink
fix(ng-class): remove previously registered watch
Browse files Browse the repository at this point in the history
Closes #725
  • Loading branch information
vicb authored and mhevery committed Mar 13, 2014
1 parent 51ee329 commit 8b54f5e
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions lib/directive/ng_class.dart
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,11 @@ abstract class _NgClassBase {
final NgAnimate _animate;
var previousSet = [];
var currentSet = [];
Watch _watch;


_NgClassBase(this.element, this.scope, this.mode, this.nodeAttrs, this._parser, this._animate)
{
_NgClassBase(this.element, this.scope, this.mode, this.nodeAttrs,
this._parser, this._animate)
{
var prevClass;

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

set valueExpression(currentExpression) {
// this should be called only once, so we don't worry about cleaning up
// watcher registrations.
scope.watch(_parser(currentExpression, collection: true), (current, _) {
if (_watch != null) _watch.remove();
_watch = scope.watch(_parser(currentExpression, collection: true), (current, _) {
currentSet = _flatten(current);
_handleChange(scope.context[r'$index']);
}, readOnly: true);
Expand All @@ -177,10 +177,8 @@ abstract class _NgClassBase {
if (oldIndex == null || mod != oldIndex % 2) {
if (mod == mode) {
currentSet.forEach((css) => _animate.addClass(element, css));
//element.classes.addAll(currentSet);
} else {
previousSet.forEach((css) => _animate.removeClass(element, css));
//element.classes.removeAll(previousSet);
}
}
}, readOnly: true);
Expand All @@ -191,18 +189,18 @@ abstract class _NgClassBase {
if (mode == null || (index != null && index % 2 == mode)) {
previousSet.forEach((css) {
if (!currentSet.contains(css)) {
_animate.removeClass(element, css);
_animate.removeClass(element, css);
} else {
element.classes.remove(css);
}
});

currentSet.forEach((css) {
if (!previousSet.contains(css)) {
_animate.addClass(element, css);
_animate.addClass(element, css);
} else {
element.classes.add(css);
}
}
});
}

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

0 comments on commit 8b54f5e

Please sign in to comment.