diff --git a/src/lib/template/dom-repeat.html b/src/lib/template/dom-repeat.html index 86d8bd4b96..37c2cede7c 100644 --- a/src/lib/template/dom-repeat.html +++ b/src/lib/template/dom-repeat.html @@ -137,7 +137,7 @@ /** * The name of the variable to add to the binding scope with the index - * for the row. If `sort` is provided, the index will reflect the + * for the inst. If `sort` is provided, the index will reflect the * sorted order (rather than the original array order). */ indexAs: { @@ -200,22 +200,22 @@ ], created: function() { - this.rows = []; + this._instances = []; }, detached: function() { - if (this.rows) { - for (var i=0; i=0 ; i--) { - var idx = removedRows[i]; + // Remove & pool removed instances + if (removedIdxs.length) { + // Sort removed instances idx's then remove backwards, + // so we don't invalidate inst index + removedIdxs.sort(); + for (var i=removedIdxs.length-1; i>=0 ; i--) { + var idx = removedIdxs[i]; // Removed idx may be undefined if item was previously filtered out if (idx !== undefined) { pool.push(this._detachRow(idx)); - rows.splice(idx, 1); + instances.splice(idx, 1); keys.splice(idx, 1); } } } - // Add rows for added keys + // Add instances for added keys if (addedKeys.length) { // Filter added keys if (this._filterFn) { @@ -451,24 +451,24 @@ addedKeys.sort(function(a, b) { return this._sortFn(c.getItem(a), c.getItem(b)); }.bind(this)); - // Insertion-sort new rows into place (from pool or newly created) + // Insertion-sort new instances into place (from pool or newly created) var start = 0; for (var i=0; i> 1; - var midKey = this._orderedKeys[mid]; + var midKey = this._keys[mid]; var cmp = sortFn(c.getItem(midKey), item); if (cmp < 0) { start = mid + 1; @@ -482,77 +482,77 @@ if (idx < 0) { idx = end + 1; } - // Insert key & row at insertion point - this._orderedKeys.splice(idx, 0, key); - this.rows.splice(idx, 0, this._insertRow(idx, pool, key)); + // Insert key & inst at insertion point + this._keys.splice(idx, 0, key); + this._instances.splice(idx, 0, this._insertRow(idx, pool, key)); return idx; }, - _applySplicesArraySort: function(splices) { - var keys = this._orderedKeys; + _applySplicesArrayOrder: function(splices) { + var keys = this._keys; var pool = []; var c = this.collection; splices.forEach(function(s) { // Apply splices to keys var args = [s.index, s.removed.length].concat(s.added); keys.splice.apply(keys, args); - // Detach & pool removed rows + // Detach & pool removed instances for (var i=0; i change up to host - _forwardInstanceProp: function(row, prop, value) { + // for notifying items. change up to host + _forwardInstanceProp: function(inst, prop, value) { if (prop == this.as) { var idx; if (this._sortFn || this._filterFn) { @@ -584,10 +584,10 @@ // efficiently memoize the array index and keep it in sync with array // mutations, so we need to look the item up in the array // This can happen e.g. when array of strings is repeated into inputs - idx = this.items.indexOf(this.collection.getItem(row.__key__)); + idx = this.items.indexOf(this.collection.getItem(inst.__key__)); } else { // When there is no sort/filter, the view index is the array index - idx = row[this.indexAs]; + idx = inst[this.indexAs]; } this.set('items.' + idx, value); } @@ -595,50 +595,50 @@ // Implements extension point from Templatizer // Called as a side effect of a template instance path change, responsible - // for notifying items.. change up to host - _forwardInstancePath: function(row, path, value) { + // for notifying items.. change up to host + _forwardInstancePath: function(inst, path, value) { if (path.indexOf(this.as + '.') === 0) { - this.notifyPath('items.' + row.__key__ + '.' + + this.notifyPath('items.' + inst.__key__ + '.' + path.slice(this.as.length + 1), value); } }, // Implements extension point from Templatizer mixin // Called as side-effect of a host property change, responsible for - // notifying parent path change on each row + // notifying parent path change on each inst _forwardParentProp: function(prop, value) { - if (this.rows) { - this.rows.forEach(function(row) { - row.__setProperty(prop, value, true); + if (this._instances) { + this._instances.forEach(function(inst) { + inst.__setProperty(prop, value, true); }, this); } }, // Implements extension point from Templatizer // Called as side-effect of a host path change, responsible for - // notifying parent path change on each row + // notifying parent path change on each inst _forwardParentPath: function(path, value) { - if (this.rows) { - this.rows.forEach(function(row) { - row.notifyPath(path, value, true); + if (this._instances) { + this._instances.forEach(function(inst) { + inst.notifyPath(path, value, true); }, this); } }, // Called as a side effect of a host items.. path change, - // responsible for notifying item. changes to row for key + // responsible for notifying item. changes to inst for key _forwardItemPath: function(path, value) { - if (this._rowForKey) { + if (this._keyToInstIdx) { var dot = path.indexOf('.'); var key = path.substring(0, dot < 0 ? path.length : dot); - var idx = this._rowForKey[key]; - var row = this.rows[idx]; - if (row) { + var idx = this._keyToInstIdx[key]; + var inst = this._instances[idx]; + if (inst) { if (dot >= 0) { path = this.as + '.' + path.substring(dot+1); - row.notifyPath(path, value, true); + inst.notifyPath(path, value, true); } else { - row.__setProperty(this.as, value, true); + inst.__setProperty(this.as, value, true); } } } @@ -675,7 +675,7 @@ }, /** - * Returns the row index for a given element stamped by this `dom-repeat`. + * Returns the inst index for a given element stamped by this `dom-repeat`. * If `sort` is provided, the index will reflect the sorted order (rather * than the original array order). * diff --git a/test/unit/dom-repeat.html b/test/unit/dom-repeat.html index 37f1a4ed9c..3739ff1a2c 100644 --- a/test/unit/dom-repeat.html +++ b/test/unit/dom-repeat.html @@ -3160,11 +3160,11 @@

inDocumentRepeater

var stamped2 = Polymer.dom(unconfigured2.root).querySelectorAll('*:not(template)'); var old = unconfigured.items[0]; // Change first item - unconfigured1.$.repeater.rows[0].itema = {prop: 'change!'}; + unconfigured1.$.repeater._instances[0].itema = {prop: 'change!'}; assert.equal(stamped1[0].itemaProp, 'change!'); assert.equal(stamped2[0].itemaProp, 'change!'); // Revert - unconfigured2.$.repeater.rows[0].itema = old; + unconfigured2.$.repeater._instances[0].itema = old; assert.equal(stamped1[0].itemaProp, 'prop-1'); assert.equal(stamped2[0].itemaProp, 'prop-1'); }); @@ -3178,11 +3178,11 @@

inDocumentRepeater

var stamped2 = Polymer.dom(unconfigured2.root).querySelectorAll('*:not(template)'); var old = unconfigured.items[2]; // Change last item (first rendered in #1, last rendered in #2) - unconfigured1.$.repeater.rows[0].itema = {prop: 'change!'}; + unconfigured1.$.repeater._instances[0].itema = {prop: 'change!'}; assert.equal(stamped1[0].itemaProp, 'change!'); assert.equal(stamped2[26].itemaProp, 'change!'); // Revert - unconfigured2.$.repeater.rows[2].itema = old; + unconfigured2.$.repeater._instances[2].itema = old; assert.equal(stamped1[0].itemaProp, 'prop-3'); assert.equal(stamped2[26].itemaProp, 'prop-3'); unconfigured1.$.repeater.sort = null;