diff options
| author | Igor Minar | 2012-11-23 22:46:58 +0100 |
|---|---|---|
| committer | Igor Minar | 2012-11-26 21:33:45 +0100 |
| commit | 6f18adedef60ddb972a446046270f98c4ac18bb7 (patch) | |
| tree | 2080308f571919c6b699c9ce70f591302ff38ea8 /src/ng/directive/ngClass.js | |
| parent | 6ad894cd5831fee19b60e57563bfee7a3f0ec995 (diff) | |
| download | angular.js-6f18adedef60ddb972a446046270f98c4ac18bb7.tar.bz2 | |
fix(ngClassOdd/ngClassEven): support shrinking/reordering in repeaters
We need to watch $index in addition to cssClasses because only then
we can correctly respond to shrinking or reordered repeaters.
Closes #1076
Diffstat (limited to 'src/ng/directive/ngClass.js')
| -rw-r--r-- | src/ng/directive/ngClass.js | 56 |
1 files changed, 43 insertions, 13 deletions
diff --git a/src/ng/directive/ngClass.js b/src/ng/directive/ngClass.js index 13b0378a..76b0ed57 100644 --- a/src/ng/directive/ngClass.js +++ b/src/ng/directive/ngClass.js @@ -3,25 +3,55 @@ function classDirective(name, selector) { name = 'ngClass' + name; return ngDirective(function(scope, element, attr) { - // Reusable function for re-applying the ngClass - function ngClassWatchAction(newVal, oldVal) { - if (selector === true || scope.$index % 2 === selector) { - if (oldVal && (newVal !== oldVal)) { - if (isObject(oldVal) && !isArray(oldVal)) - oldVal = map(oldVal, function(v, k) { if (v) return k }); - element.removeClass(isArray(oldVal) ? oldVal.join(' ') : oldVal); - } - if (isObject(newVal) && !isArray(newVal)) - newVal = map(newVal, function(v, k) { if (v) return k }); - if (newVal) element.addClass(isArray(newVal) ? newVal.join(' ') : newVal); - } - }; + scope.$watch(attr[name], ngClassWatchAction, true); attr.$observe('class', function(value) { var ngClass = scope.$eval(attr[name]); ngClassWatchAction(ngClass, ngClass); }); + + + if (name !== 'ngClass') { + scope.$watch('$index', function($index, old$index) { + var mod = $index % 2; + if (mod !== old$index % 2) { + if (mod == selector) { + addClass(scope.$eval(attr[name])); + } else { + removeClass(scope.$eval(attr[name])); + } + } + }); + } + + + function ngClassWatchAction(newVal, oldVal) { + if (selector === true || scope.$index % 2 === selector) { + if (oldVal && (newVal !== oldVal)) { + removeClass(oldVal); + } + addClass(newVal); + } + } + + + function removeClass(classVal) { + if (isObject(classVal) && !isArray(classVal)) { + classVal = map(classVal, function(v, k) { if (v) return k }); + } + element.removeClass(isArray(classVal) ? classVal.join(' ') : classVal); + } + + + function addClass(classVal) { + if (isObject(classVal) && !isArray(classVal)) { + classVal = map(classVal, function(v, k) { if (v) return k }); + } + if (classVal) { + element.addClass(isArray(classVal) ? classVal.join(' ') : classVal); + } + } }); } |
