Skip to content

Commit

Permalink
Support property observers which are direct function references in ad…
Browse files Browse the repository at this point in the history
…dition to strings.

Provides better static analysis and refactoring support in multiple tools. Alleviates the need for property reflection with Closure-compiler renaming.
  • Loading branch information
ChadKillingsworth committed Apr 29, 2017
1 parent a284d77 commit 99e6bda
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
14 changes: 7 additions & 7 deletions lib/mixins/property-effects.html
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,12 @@
* @private
*/
function runObserverEffect(inst, property, props, oldProps, info) {
let fn = inst[info.methodName];
let fn = typeof info.method === 'function' ? info.method : inst[info.method];
let changedProp = info.property;
if (fn) {
fn.call(inst, inst.__data[changedProp], oldProps[changedProp]);
} else {
console.warn('observer method `' + info.methodName + '` not defined');
console.warn('observer method `' + info.method + '` not defined');
}
}

Expand Down Expand Up @@ -1899,19 +1899,19 @@
* Creates a single-property observer for the given property.
*
* @param {string} property Property name
* @param {string} methodName Name of observer method to call
* @param {string|function(*,*)} method Name or function reference of observer method to call
* @param {boolean=} dynamicFn Whether the method name should be included as
* a dependency to the effect.
* @protected
*/
_createPropertyObserver(property, methodName, dynamicFn) {
let info = { property, methodName };
_createPropertyObserver(property, method, dynamicFn) {
let info = { property, method };
this._addPropertyEffect(property, TYPES.OBSERVE, {
fn: runObserverEffect, info, trigger: {name: property}
});
if (dynamicFn) {
this._addPropertyEffect(methodName, TYPES.OBSERVE, {
fn: runObserverEffect, info, trigger: {name: methodName}
this._addPropertyEffect(method, TYPES.OBSERVE, {
fn: runObserverEffect, info, trigger: {name: method}
});
}
}
Expand Down
6 changes: 4 additions & 2 deletions test/unit/property-effects-elements.html
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@
</script>

<script>
var prop2Changed = sinon.spy();
Polymer({
is: 'x-prop',
properties: {
Expand All @@ -442,12 +443,13 @@
},
prop2: {
value: 'default',
observer: 'prop2Changed'
observer: prop2Changed
}
},
created: function() {
this.prop1Changed = sinon.spy();
this.prop2Changed = sinon.spy();
this.prop2Changed = prop2Changed;
prop2Changed.reset();
}
});
</script>
Expand Down

0 comments on commit 99e6bda

Please sign in to comment.