Skip to content

Commit

Permalink
Merge pull request #2409 from Polymer/2408-multiple-linkPath
Browse files Browse the repository at this point in the history
Allow multiple paths to be linked using linkPath. Fixes #2048
  • Loading branch information
Steve Orvell committed Sep 5, 2015
2 parents 573ca29 + bee110b commit 83cce66
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 16 deletions.
18 changes: 4 additions & 14 deletions src/standard/notify-path.html
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@
this._boundPaths[to] = from;
// this.set(to, this.get(from));
} else {
this.unbindPath(to);
this.unlinkPaths(to);
// this.set(to, from);
}
},
Expand All @@ -299,23 +299,13 @@
},

_notifyBoundPaths: function(path, value) {
var from, to;
for (var a in this._boundPaths) {
var b = this._boundPaths[a];
if (path.indexOf(a + '.') == 0) {
from = a;
to = b;
break;
this.notifyPath(this._fixPath(b, a, path), value);
} else if (path.indexOf(b + '.') == 0) {
this.notifyPath(this._fixPath(a, b, path), value);
}
if (path.indexOf(b + '.') == 0) {
from = b;
to = a;
break;
}
}
if (from && to) {
var p = this._fixPath(to, from, path);
this.notifyPath(p, value);
}
},

Expand Down
10 changes: 8 additions & 2 deletions test/unit/notify-path-elements.html
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@
'multiplePathsChanged(a, nested.b, nested.obj.c)',
'arrayChanged(array.splices)',
'arrayNoCollChanged(arrayNoColl.splices)',
'arrayOrPropChanged(prop, array.splices)'
'arrayOrPropChanged(prop, array.splices)',
'aChanged(a.*)',
'bChanged(b.*)',
'cChanged(c.*)'
],
created: function() {
this.observerCounts = {
Expand Down Expand Up @@ -219,7 +222,10 @@
arrayOrPropChanged: function(prop, splices) {
this.observerCounts.arrayOrPropChanged++;
assert.equal(prop, this.prop);
}
},
aChanged: function() {},
bChanged: function() {},
cChanged: function() {}
});
</script>
</dom-module>
76 changes: 76 additions & 0 deletions test/unit/notify-path.html
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,82 @@
assert.strictEqual(el.array[3], 'orig3');
});

test('link two objects', function() {
var aChanged = 0;
var bChanged = 0;
el.a = el.b = {};
el.linkPaths('b', 'a');
el.aChanged = function() { aChanged++; };
el.bChanged = function() { bChanged++; };
el.set('a.foo', 1);
assert.equal(aChanged, 1);
assert.equal(bChanged, 1);
el.unlinkPaths('b');
el.set('a.foo', 2);
assert.equal(aChanged, 2);
assert.equal(bChanged, 1);
});

test('link three objects', function() {
var aChanged = 0;
var bChanged = 0;
var cChanged = 0;
el.a = el.b = el.c = {};
el.linkPaths('b', 'a');
el.linkPaths('c', 'a');
el.aChanged = function() { aChanged++; };
el.bChanged = function() { bChanged++; };
el.cChanged = function() { cChanged++; };
el.set('a.foo', 1);
assert.equal(aChanged, 1);
assert.equal(bChanged, 1);
assert.equal(cChanged, 1);
el.unlinkPaths('b');
el.set('a.foo', 2);
assert.equal(aChanged, 2);
assert.equal(bChanged, 1);
assert.equal(cChanged, 2);
});

test('link two arrays', function() {
var aChanged = 0;
var bChanged = 0;
el.a = el.b = [];
el.linkPaths('b', 'a');
el.aChanged = function() { aChanged++; };
el.bChanged = function() { bChanged++; };
el.push('a', {});
// 2 changes for arrays (splices & length)
assert.equal(aChanged, 2);
assert.equal(bChanged, 2);
el.unlinkPaths('b');
el.push('a', {});
assert.equal(aChanged, 4);
assert.equal(bChanged, 2);
});

test('link three arrays', function() {
var aChanged = 0;
var bChanged = 0;
var cChanged = 0;
el.a = el.b = el.c = [];
el.linkPaths('b', 'a');
el.linkPaths('c', 'a');
el.aChanged = function() { aChanged++; };
el.bChanged = function() { bChanged++; };
el.cChanged = function() { cChanged++; };
el.push('a', {});
// 2 changes for arrays (splices & length)
assert.equal(aChanged, 2);
assert.equal(bChanged, 2);
assert.equal(cChanged, 2);
el.unlinkPaths('b');
el.push('a', {});
assert.equal(aChanged, 4);
assert.equal(bChanged, 2);
assert.equal(cChanged, 4);
});

});

</script>
Expand Down

0 comments on commit 83cce66

Please sign in to comment.