Skip to content

Commit 4bae2b6

Browse files
Support property observers which are direct function references in addition to strings.
Provides better static analysis and refactoring support in multiple tools. Alleviates the need for property reflection with Closure-compiler renaming.
1 parent 9b00efa commit 4bae2b6

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

lib/mixins/property-effects.html

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -208,12 +208,12 @@
208208
* @private
209209
*/
210210
function runObserverEffect(inst, property, props, oldProps, info) {
211-
let fn = inst[info.methodName];
211+
let fn = typeof info.method === "string" ? inst[info.method] : info.method;
212212
let changedProp = info.property;
213213
if (fn) {
214214
fn.call(inst, inst.__data[changedProp], oldProps[changedProp]);
215215
} else if (!info.dynamicFn) {
216-
console.warn('observer method `' + info.methodName + '` not defined');
216+
console.warn('observer method `' + info.method + '` not defined');
217217
}
218218
}
219219

@@ -1989,19 +1989,19 @@
19891989
* full API docs.
19901990
*
19911991
* @param {string} property Property name
1992-
* @param {string} methodName Name of observer method to call
1992+
* @param {string|function(*,*)} method Function or name of observer method to call
19931993
* @param {boolean=} dynamicFn Whether the method name should be included as
19941994
* a dependency to the effect.
19951995
* @protected
19961996
*/
1997-
_createPropertyObserver(property, methodName, dynamicFn) {
1998-
let info = { property, methodName, dynamicFn: Boolean(dynamicFn) };
1997+
_createPropertyObserver(property, method, dynamicFn) {
1998+
let info = { property, method, dynamicFn: Boolean(dynamicFn) };
19991999
this._addPropertyEffect(property, TYPES.OBSERVE, {
20002000
fn: runObserverEffect, info, trigger: {name: property}
20012001
});
20022002
if (dynamicFn) {
2003-
this._addPropertyEffect(methodName, TYPES.OBSERVE, {
2004-
fn: runObserverEffect, info, trigger: {name: methodName}
2003+
this._addPropertyEffect(method, TYPES.OBSERVE, {
2004+
fn: runObserverEffect, info, trigger: {name: method}
20052005
});
20062006
}
20072007
}
@@ -2129,13 +2129,13 @@
21292129
* Creates a single-property observer for the given property.
21302130
*
21312131
* @param {string} property Property name
2132-
* @param {string} methodName Name of observer method to call
2132+
* @param {string|function(*,*)} method Function or name of observer method to call
21332133
* @param {boolean=} dynamicFn Whether the method name should be included as
21342134
* a dependency to the effect.
21352135
* @protected
21362136
*/
2137-
static createPropertyObserver(property, methodName, dynamicFn) {
2138-
this.prototype._createPropertyObserver(property, methodName, dynamicFn);
2137+
static createPropertyObserver(property, method, dynamicFn) {
2138+
this.prototype._createPropertyObserver(property, method, dynamicFn);
21392139
}
21402140

21412141
/**

test/unit/property-effects-elements.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@
496496
},
497497
prop2: {
498498
value: 'default',
499-
observer: 'prop2Changed'
499+
observer: function(newProp, oldProp) { return this.prop2Changed(newProp, oldProp); }
500500
}
501501
},
502502
created: function() {

0 commit comments

Comments
 (0)