Skip to content

Commit

Permalink
Merge pull request #4574 from ChadKillingsworth/observer-function
Browse files Browse the repository at this point in the history
Support property observers which are direct function references
  • Loading branch information
TimvdLippe authored Dec 1, 2017
2 parents b124b70 + 4bae2b6 commit 0c185b5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
20 changes: 10 additions & 10 deletions lib/mixins/property-effects.html
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,12 @@
* @private
*/
function runObserverEffect(inst, property, props, oldProps, info) {
let fn = inst[info.methodName];
let fn = typeof info.method === "string" ? inst[info.method] : info.method;
let changedProp = info.property;
if (fn) {
fn.call(inst, inst.__data[changedProp], oldProps[changedProp]);
} else if (!info.dynamicFn) {
console.warn('observer method `' + info.methodName + '` not defined');
console.warn('observer method `' + info.method + '` not defined');
}
}

Expand Down Expand Up @@ -2023,20 +2023,20 @@
* full API docs.
*
* @param {string} property Property name
* @param {string} methodName Name of observer method to call
* @param {string|function(*,*)} method Function or name of observer method to call
* @param {boolean=} dynamicFn Whether the method name should be included as
* a dependency to the effect.
* @return {void}
* @protected
*/
_createPropertyObserver(property, methodName, dynamicFn) {
let info = { property, methodName, dynamicFn: Boolean(dynamicFn) };
_createPropertyObserver(property, method, dynamicFn) {
let info = { property, method, dynamicFn: Boolean(dynamicFn) };
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 Expand Up @@ -2169,14 +2169,14 @@
* 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 Function or name of observer method to call
* @param {boolean=} dynamicFn Whether the method name should be included as
* a dependency to the effect.
* @return {void}
* @protected
*/
static createPropertyObserver(property, methodName, dynamicFn) {
this.prototype._createPropertyObserver(property, methodName, dynamicFn);
static createPropertyObserver(property, method, dynamicFn) {
this.prototype._createPropertyObserver(property, method, dynamicFn);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion test/unit/property-effects-elements.html
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@
},
prop2: {
value: 'default',
observer: 'prop2Changed'
observer: function(newProp, oldProp) { return this.prop2Changed(newProp, oldProp); }
}
},
created: function() {
Expand Down

0 comments on commit 0c185b5

Please sign in to comment.