mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2026-03-13 10:22:08 +08:00
fix(collectionRepeat): simplify item reusing process to fix rare reuse error
Closes #1777.
This commit is contained in:
72
js/angular/service/collectionRepeatDataSource.js
vendored
72
js/angular/service/collectionRepeatDataSource.js
vendored
@@ -25,26 +25,14 @@ function($cacheFactory, $parse, $rootScope) {
|
||||
this.dimensions = [];
|
||||
this.data = [];
|
||||
|
||||
if (this.trackByExpr) {
|
||||
var trackByGetter = $parse(this.trackByExpr);
|
||||
var hashFnLocals = {$id: hashKey};
|
||||
this.itemHashGetter = function(index, value) {
|
||||
hashFnLocals[self.keyExpr] = value;
|
||||
hashFnLocals.$index = index;
|
||||
return trackByGetter(self.scope, hashFnLocals);
|
||||
};
|
||||
} else {
|
||||
this.itemHashGetter = function(index, value) {
|
||||
return hashKey(value);
|
||||
};
|
||||
}
|
||||
|
||||
this.attachedItems = {};
|
||||
this.BACKUP_ITEMS_LENGTH = 10;
|
||||
this.BACKUP_ITEMS_LENGTH = 20;
|
||||
this.backupItemsArray = [];
|
||||
}
|
||||
CollectionRepeatDataSource.prototype = {
|
||||
setup: function() {
|
||||
if (this.isSetup) return;
|
||||
this.isSetup = true;
|
||||
for (var i = 0; i < this.BACKUP_ITEMS_LENGTH; i++) {
|
||||
this.detachItem(this.createItem());
|
||||
}
|
||||
@@ -70,19 +58,18 @@ function($cacheFactory, $parse, $rootScope) {
|
||||
},
|
||||
createItem: function() {
|
||||
var item = {};
|
||||
item.scope = this.scope.$new();
|
||||
|
||||
item.scope = this.scope.$new();
|
||||
this.transcludeFn(item.scope, function(clone) {
|
||||
clone.css('position', 'absolute');
|
||||
item.element = clone;
|
||||
});
|
||||
|
||||
this.transcludeParent.append(item.element);
|
||||
|
||||
return item;
|
||||
},
|
||||
getItem: function(hash) {
|
||||
if ( (item = this.attachedItems[hash]) ) {
|
||||
getItem: function(index) {
|
||||
if ( (item = this.attachedItems[index]) ) {
|
||||
//do nothing, the item is good
|
||||
} else if ( (item = this.backupItemsArray.pop()) ) {
|
||||
reconnectScope(item.scope);
|
||||
@@ -92,20 +79,18 @@ function($cacheFactory, $parse, $rootScope) {
|
||||
return item;
|
||||
},
|
||||
attachItemAtIndex: function(index) {
|
||||
var value = this.data[index];
|
||||
|
||||
if (index < this.dataStartIndex) {
|
||||
return this.beforeSiblings[index];
|
||||
} else if (index > this.data.length) {
|
||||
return this.afterSiblings[index - this.data.length - this.dataStartIndex];
|
||||
}
|
||||
|
||||
var hash = this.itemHashGetter(index, value);
|
||||
var item = this.getItem(hash);
|
||||
var item = this.getItem(index);
|
||||
var value = this.data[index];
|
||||
|
||||
if (item.scope.$index !== index || item.scope[this.keyExpr] !== value) {
|
||||
if (item.index !== index || item.scope[this.keyExpr] !== value) {
|
||||
item.index = item.scope.$index = index;
|
||||
item.scope[this.keyExpr] = value;
|
||||
item.scope.$index = index;
|
||||
item.scope.$first = (index === 0);
|
||||
item.scope.$last = (index === (this.getLength() - 1));
|
||||
item.scope.$middle = !(item.scope.$first || item.scope.$last);
|
||||
@@ -116,9 +101,7 @@ function($cacheFactory, $parse, $rootScope) {
|
||||
item.scope.$digest();
|
||||
}
|
||||
}
|
||||
|
||||
item.hash = hash;
|
||||
this.attachedItems[hash] = item;
|
||||
this.attachedItems[index] = item;
|
||||
|
||||
return item;
|
||||
},
|
||||
@@ -129,13 +112,12 @@ function($cacheFactory, $parse, $rootScope) {
|
||||
item.element = null;
|
||||
},
|
||||
detachItem: function(item) {
|
||||
delete this.attachedItems[item.hash];
|
||||
delete this.attachedItems[item.index];
|
||||
|
||||
//If it's an outside item, only hide it. These items aren't part of collection
|
||||
//repeat's list, only sit outside
|
||||
if (item.isOutside) {
|
||||
hideWithTransform(item.element);
|
||||
|
||||
// If we are at the limit of backup items, just get rid of the this element
|
||||
} else if (this.backupItemsArray.length >= this.BACKUP_ITEMS_LENGTH) {
|
||||
this.destroyItem(item);
|
||||
@@ -167,36 +149,6 @@ function($cacheFactory, $parse, $rootScope) {
|
||||
return CollectionRepeatDataSource;
|
||||
}]);
|
||||
|
||||
/**
|
||||
* Computes a hash of an 'obj'.
|
||||
* Hash of a:
|
||||
* string is string
|
||||
* number is number as string
|
||||
* object is either result of calling $$hashKey function on the object or uniquely generated id,
|
||||
* that is also assigned to the $$hashKey property of the object.
|
||||
*
|
||||
* @param obj
|
||||
* @returns {string} hash string such that the same input will have the same hash string.
|
||||
* The resulting string key is in 'type:hashKey' format.
|
||||
*/
|
||||
function hashKey(obj) {
|
||||
var objType = typeof obj,
|
||||
key;
|
||||
|
||||
if (objType == 'object' && obj !== null) {
|
||||
if (typeof (key = obj.$$hashKey) == 'function') {
|
||||
// must invoke on object to keep the right this
|
||||
key = obj.$$hashKey();
|
||||
} else if (key === undefined) {
|
||||
key = obj.$$hashKey = ionic.Utils.nextUid();
|
||||
}
|
||||
} else {
|
||||
key = obj;
|
||||
}
|
||||
|
||||
return objType + ':' + key;
|
||||
}
|
||||
|
||||
function disconnectScope(scope) {
|
||||
if (scope.$root === scope) {
|
||||
return; // we can't disconnect the root node;
|
||||
|
||||
14
js/angular/service/collectionRepeatManager.js
vendored
14
js/angular/service/collectionRepeatManager.js
vendored
@@ -155,9 +155,7 @@ function($rootScope, $timeout) {
|
||||
this.beforeSize = result.beforeSize;
|
||||
this.setCurrentIndex(0);
|
||||
this.render(true);
|
||||
if (!this.dataSource.backupItemsArray.length) {
|
||||
this.dataSource.setup();
|
||||
}
|
||||
this.dataSource.setup();
|
||||
},
|
||||
/*
|
||||
* setCurrentIndex sets the index in the list that matches the scroller's position.
|
||||
@@ -194,6 +192,7 @@ function($rootScope, $timeout) {
|
||||
}
|
||||
return this.scrollView.__$callback(transformLeft, transformTop, zoom, wasResize);
|
||||
}),
|
||||
|
||||
renderIfNeeded: function(scrollPos) {
|
||||
if ((this.hasNextIndex && scrollPos >= this.nextPos) ||
|
||||
(this.hasPrevIndex && scrollPos < this.previousPos)) {
|
||||
@@ -334,15 +333,6 @@ function($rootScope, $timeout) {
|
||||
}
|
||||
};
|
||||
|
||||
var exceptions = {'renderScroll':1, 'renderIfNeeded':1};
|
||||
forEach(CollectionRepeatManager.prototype, function(method, key) {
|
||||
if (exceptions[key]) return;
|
||||
CollectionRepeatManager.prototype[key] = function() {
|
||||
console.log(key + '(', arguments, ')');
|
||||
return method.apply(this, arguments);
|
||||
};
|
||||
});
|
||||
|
||||
return CollectionRepeatManager;
|
||||
}]);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user