Avoid non-bubbling event gotchas with binding.

This commit is contained in:
Kevin Schaaf
2015-03-19 17:55:41 -07:00
parent e2264bc2ad
commit cd362bb891
3 changed files with 75 additions and 5 deletions

View File

@@ -23,7 +23,7 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
_addAnnotatedListener: function(model, index, property, path) {
// <node>.on.<property>-changed: <path> = e.detail.value
//
var changedFn = '\tthis.' + path + ' = e.detail.value;';
var changedFn = '\t\tthis.' + path + ' = e.detail.value;';
if (path.indexOf('.') > 0) {
// TODO(kschaaf): dirty check avoids null references when the object has gone away
changedFn =
@@ -32,12 +32,15 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
'\t\tthis.notifyPath(\'' + path + '\', e.detail.value)\n' +
'\t}';
}
changedFn = 'if (e.detail.path) {\n' +
'\tvar path = this._fixPath(\'' + path + '\', \'' +
changedFn =
'if (!e.path || e.path[0] == e.target) {\n' +
'\tif (e.detail.path) {\n' +
'\t\tvar path = this._fixPath(\'' + path + '\', \'' +
property + '\', e.detail.path);\n' +
'\tthis.notifyPath(path, e.detail.value);\n' +
'} else {\n' +
'\t\tthis.notifyPath(path, e.detail.value);\n' +
'\t} else {\n' +
changedFn + '\n' +
'\t}\n' +
'}';
//
model._bindListeners.push({

View File

@@ -143,3 +143,47 @@
}
});
</script>
<script>
Polymer({
is: 'x-notifies1',
properties: {
notifies: {
notify: true
}
}
});
</script>
<dom-module id="x-notifies2">
<template>
<x-notifies1 id="notifies1"></x-notifies1>
</template>
</dom-module>
<script>
Polymer({
is: 'x-notifies2',
properties: {
notifies: {
notify: true
}
}
});
</script>
<dom-module id="x-notifies3">
<template>
<x-notifies2 id="notifies2" notifies="{{shouldNotChange}}"></x-notifies2>
</template>
</dom-module>
<script>
Polymer({
is: 'x-notifies3',
properties: {
shouldNotChange: {
observer: 'shouldNotChangeChanged'
}
},
shouldNotChangeChanged: function() { }
});
</script>

View File

@@ -593,6 +593,29 @@ suite('binding to attribute', function() {
});
suite('avoid non-bubbling event gotchas', function() {
var el;
beforeEach(function() {
el = document.createElement('x-notifies3');
document.body.appendChild(el);
});
afterEach(function() {
document.body.removeChild(el);
});
test('avoid non-bubbling event gotchas', function() {
el.shouldNotChangeChanged = function(e) {
assert(false, 'parent notifies should not have changed');
};
el.$.notifies2.$.notifies1.notifies = 42;
});
});
</script>