Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Repeater fixes #1602

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/apis.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,12 @@ HashQueueMap.prototype = {
}
}
},

/**
* return the first item without deleting it
*/
peek: function(key) {
var array = this[key = hashKey(key)];
var array = this[hashKey(key)];
if (array) {
return array[0];
}
Expand Down
12 changes: 6 additions & 6 deletions src/jqLite.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,9 @@ function JQLiteHasClass(element, selector) {
indexOf( " " + selector + " " ) > -1);
}

function JQLiteRemoveClass(element, selector) {
if (selector) {
forEach(selector.split(' '), function(cssClass) {
function JQLiteRemoveClass(element, cssClasses) {
if (cssClasses) {
forEach(cssClasses.split(' '), function(cssClass) {
element.className = trim(
(" " + element.className + " ")
.replace(/[\n\t]/g, " ")
Expand All @@ -278,9 +278,9 @@ function JQLiteRemoveClass(element, selector) {
}
}

function JQLiteAddClass(element, selector) {
if (selector) {
forEach(selector.split(' '), function(cssClass) {
function JQLiteAddClass(element, cssClasses) {
if (cssClasses) {
forEach(cssClasses.split(' '), function(cssClass) {
if (!JQLiteHasClass(element, cssClass)) {
element.className = trim(element.className + ' ' + trim(cssClass));
}
Expand Down
27 changes: 15 additions & 12 deletions src/ng/directive/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -1010,22 +1010,25 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$

// model -> value
var ctrl = this;
$scope.$watch(ngModelGet, function ngModelWatchAction(value) {

// ignore change from view
if (ctrl.$modelValue === value) return;
$scope.$watch(function ngModelWatch() {
var value = ngModelGet($scope);

var formatters = ctrl.$formatters,
idx = formatters.length;
// if scope model value and ngModel value are out of sync
if (ctrl.$modelValue !== value) {

ctrl.$modelValue = value;
while(idx--) {
value = formatters[idx](value);
}
var formatters = ctrl.$formatters,
idx = formatters.length;

if (ctrl.$viewValue !== value) {
ctrl.$viewValue = value;
ctrl.$render();
ctrl.$modelValue = value;
while(idx--) {
value = formatters[idx](value);
}

if (ctrl.$viewValue !== value) {
ctrl.$viewValue = value;
ctrl.$render();
}
}
});
}];
Expand Down
56 changes: 43 additions & 13 deletions src/ng/directive/ngClass.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
});
}

Expand Down
27 changes: 2 additions & 25 deletions src/ng/directive/ngRepeat.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ var ngRepeatDirective = ngDirective({
// We need an array of these objects since the same object can be returned from the iterator.
// We expect this to be a rare case.
var lastOrder = new HashQueueMap();
var indexValues = [];

scope.$watch(function ngRepeatWatch(scope){
var index, length,
collection = scope.$eval(rhs),
Expand Down Expand Up @@ -119,18 +119,7 @@ var ngRepeatDirective = ngDirective({
key = (collection === array) ? index : array[index];
value = collection[key];

// if collection is array and value is object, it can be shifted to allow for position change
// if collection is array and value is not object, need to first check whether index is same to
// avoid shifting wrong value
// if collection is not array, need to always check index to avoid shifting wrong value
if (lastOrder.peek(value)) {
last = collection === array ?
((isObject(value)) ? lastOrder.shift(value) :
(index === lastOrder.peek(value).index ? lastOrder.shift(value) : undefined)) :
(index === lastOrder.peek(value).index ? lastOrder.shift(value) : undefined);
} else {
last = undefined;
}
last = lastOrder.shift(value);

if (last) {
// if we have already seen this object, then we need to reuse the
Expand All @@ -151,12 +140,6 @@ var ngRepeatDirective = ngDirective({
cursor = last.element;
}
} else {
if (indexValues.hasOwnProperty(index) && collection !== array) {
var preValue = indexValues[index];
var v = lastOrder.shift(preValue);
v.element.remove();
v.scope.$destroy();
}
// new item which we don't know about
childScope = scope.$new();
}
Expand All @@ -178,16 +161,10 @@ var ngRepeatDirective = ngDirective({
index: index
};
nextOrder.push(value, last);
indexValues[index] = value;
});
}
}

var i, l;
for (i = 0, l = indexValues.length - length; i < l; i++) {
indexValues.pop();
}

//shrink children
for (key in lastOrder) {
if (lastOrder.hasOwnProperty(key)) {
Expand Down
Loading