Skip to content

Commit

Permalink
Denote keys with # to disambiguate from index. Fixes #2007.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinpschaaf committed Oct 8, 2015
1 parent b2b23c4 commit 85d8a3a
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 23 deletions.
20 changes: 16 additions & 4 deletions src/lib/collection.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@
} else {
this.pmap[item] = key;
}
return key;
return '#' + key;
},

removeKey: function(key) {
key = this._parseKey(key);
this._removeFromMap(this.store[key]);
delete this.store[key];
},
Expand All @@ -70,17 +71,27 @@

getKey: function(item) {
if (item && typeof item == 'object') {
return this.omap.get(item);
return '#' + this.omap.get(item);
} else {
return this.pmap[item];
return '#' + this.pmap[item];
}
},

getKeys: function() {
return Object.keys(this.store);
return Object.keys(this.store).map(function(key) {
return '#' + key;
});
},

_parseKey: function(key) {
if (key[0] == '#') {
return key.slice(1);
}
throw new Error('unexpected key ' + key);
},

setItem: function(key, item) {
key = this._parseKey(key);
var old = this.store[key];
if (old) {
this._removeFromMap(old);
Expand All @@ -94,6 +105,7 @@
},

getItem: function(key) {
key = this._parseKey(key);
return this.store[key];
},

Expand Down
18 changes: 14 additions & 4 deletions src/standard/notify-path.html
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,20 @@
get: function(path, root) {
var prop = root || this;
var parts = this._getPathParts(path);
var last = parts.pop();
while (parts.length) {
prop = prop[parts.shift()];
var array;
for (var i=0; i<parts.length; i++) {
if (!prop) {
return;
}
var part = parts[i];
if (array && part[0] == '#') {
prop = Polymer.Collection.get(array).getItem(part);
} else {
prop = prop[part];
}
array = Array.isArray(prop) ? prop : null;
}
return prop[last];
return prop;
},

_pathEffector: function(path, value) {
Expand Down Expand Up @@ -342,6 +348,10 @@
keySplices: Polymer.Collection.applySplices(array, splices),
indexSplices: splices
};
if (!array.hasOwnProperty('splices')) {
Object.defineProperty(array, 'splices',
{configurable: true, writable: true});
}
this.set(path + '.splices', change);
if (added != removed.length) {
this.notifyPath(path + '.length', array.length);
Expand Down
2 changes: 2 additions & 0 deletions test/unit/notify-path-elements.html
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
'multipleChanged(a, b, nested.obj.*)',
'multiplePathsChanged(a, nested.b, nested.obj.c)',
'arrayChanged(array.splices)',
'arrayChangedDeep(array.*)',
'arrayNoCollChanged(arrayNoColl.splices)',
'arrayOrPropChanged(prop, array.splices)',
'aChanged(a.*)',
Expand Down Expand Up @@ -210,6 +211,7 @@
'arrayChanged keySplices incorrect');
}
},
arrayChangedDeep: function(info) { },
arrayNoCollChanged: function(splices) {
this.observerCounts.arrayNoCollChanged++;
if (this.arrayNoColl.length) {
Expand Down
62 changes: 47 additions & 15 deletions test/unit/notify-path.html
Original file line number Diff line number Diff line change
Expand Up @@ -915,8 +915,8 @@
assert.strictEqual(change.indexSplices[0].removed.length, 0);
assert.strictEqual(change.keySplices.length, 1);
assert.strictEqual(change.keySplices[0].added.length, 2);
assert.equal(change.keySplices[0].added[0], key);
assert.equal(change.keySplices[0].added[1], key+1);
assert.strictEqual(change.keySplices[0].added[0], '#' + key);
assert.strictEqual(change.keySplices[0].added[1], '#' + (key+1));
assert.strictEqual(change.keySplices[0].removed.length, 0);
};
var ret = el.push('array', 'new1', 'new2');
Expand All @@ -943,7 +943,7 @@
assert.strictEqual(change.keySplices.length, 1);
assert.strictEqual(change.keySplices[0].added.length, 0);
assert.strictEqual(change.keySplices[0].removed.length, 1);
assert.equal(change.keySplices[0].removed[0], key);
assert.strictEqual(change.keySplices[0].removed[0], '#' + key);
};
var ret = el.pop('array');
assert.strictEqual(ret, 'orig3');
Expand All @@ -964,8 +964,8 @@
assert.strictEqual(change.indexSplices[0].removed.length, 0);
assert.strictEqual(change.keySplices.length, 1);
assert.strictEqual(change.keySplices[0].added.length, 2);
assert.equal(change.keySplices[0].added[0], key);
assert.equal(change.keySplices[0].added[1], key+1);
assert.strictEqual(change.keySplices[0].added[0], '#' + key);
assert.strictEqual(change.keySplices[0].added[1], '#' + (key+1));
assert.strictEqual(change.keySplices[0].removed.length, 0);
};
var ret = el.unshift('array', 'new1', 'new2');
Expand All @@ -991,7 +991,7 @@
assert.strictEqual(change.keySplices.length, 1);
assert.strictEqual(change.keySplices[0].added.length, 0);
assert.strictEqual(change.keySplices[0].removed.length, 1);
assert.equal(change.keySplices[0].removed[0], 0);
assert.strictEqual(change.keySplices[0].removed[0], '#' + 0);
};
var ret = el.shift('array');
assert.strictEqual(ret, 'orig1');
Expand All @@ -1013,10 +1013,10 @@
assert.strictEqual(change.indexSplices[0].removed[0], 'orig2');
assert.strictEqual(change.keySplices.length, 1);
assert.strictEqual(change.keySplices[0].added.length, 2);
assert.equal(change.keySplices[0].added[0], key);
assert.equal(change.keySplices[0].added[1], key+1);
assert.strictEqual(change.keySplices[0].added[0], '#' + key);
assert.strictEqual(change.keySplices[0].added[1], '#' + (key+1));
assert.strictEqual(change.keySplices[0].removed.length, 1);
assert.equal(change.keySplices[0].removed[0], 1);
assert.strictEqual(change.keySplices[0].removed[0], '#' + 1);
};
var ret = el.splice('array', 1, 1, 'new1', 'new2');
assert.deepEqual(ret, ['orig2']);
Expand Down Expand Up @@ -1112,10 +1112,10 @@
assert.strictEqual(change.indexSplices[0].removed[0], 'orig2');
assert.strictEqual(change.keySplices.length, 1);
assert.strictEqual(change.keySplices[0].added.length, 2);
assert.equal(change.keySplices[0].added[0], key);
assert.equal(change.keySplices[0].added[1], key+1);
assert.strictEqual(change.keySplices[0].added[0], '#' + key);
assert.strictEqual(change.keySplices[0].added[1], '#' + (key+1));
assert.strictEqual(change.keySplices[0].removed.length, 1);
assert.equal(change.keySplices[0].removed[0], 1);
assert.strictEqual(change.keySplices[0].removed[0], '#' + 1);
};
var ret = el.splice('array', '1', '1', 'new1', 'new2');
assert.deepEqual(ret, ['orig2']);
Expand All @@ -1139,10 +1139,10 @@
assert.strictEqual(change.indexSplices[0].removed[0], 'orig2');
assert.strictEqual(change.keySplices.length, 1);
assert.strictEqual(change.keySplices[0].added.length, 2);
assert.equal(change.keySplices[0].added[0], key);
assert.equal(change.keySplices[0].added[1], key+1);
assert.strictEqual(change.keySplices[0].added[0], '#' + key);
assert.strictEqual(change.keySplices[0].added[1], '#' + (key+1));
assert.strictEqual(change.keySplices[0].removed.length, 1);
assert.equal(change.keySplices[0].removed[0], 1);
assert.strictEqual(change.keySplices[0].removed[0], '#' + 1);
};
var ret = el.splice('array', '-2', '1', 'new1', 'new2');
assert.deepEqual(ret, ['orig2']);
Expand All @@ -1153,6 +1153,7 @@
assert.strictEqual(el.array[3], 'orig3');
});

<<<<<<< HEAD
test('link two objects', function() {
var aChanged = 0;
var bChanged = 0;
Expand Down Expand Up @@ -1229,6 +1230,37 @@
assert.equal(cChanged, 4);
});

test('get from path in deep observer', function() {
el.arrayChanged = nop;
var array = [1, 2, 3];
// Initialize array
el.arrayChangedDeep = function(info) {
assert.strictEqual(el.get(info.path), array);
};
el.array = array;
// Change index 0
el.arrayChangedDeep = function(info) {
assert.strictEqual(el.get(info.path), 99);
};
el.set('array.0', 99);
// Unshift value
el.arrayChangedDeep = function(info) {
if (info.path == 'array.splices') {
assert.strictEqual(el.get(info.path), array.splices);
} else {
assert.strictEqual(el.get(info.path), 4);
}
};
el.unshift('array', 0);
// Change index 0
el.arrayChangedDeep = function(info) {
assert.strictEqual(el.get(info.path), -1);
};
el.set('array.0', -1);
// Verify array contents
assert.deepEqual(el.array, [-1, 99, 2, 3]);
});

});

</script>
Expand Down

0 comments on commit 85d8a3a

Please sign in to comment.