Skip to content

Commit

Permalink
implement system to watch the actual array for array-valued paths, in…
Browse files Browse the repository at this point in the history
… addition to the path itself; needs tests
  • Loading branch information
Scott J. Miles committed Oct 11, 2013
1 parent fd8bbee commit 6004350
Showing 1 changed file with 53 additions and 13 deletions.
66 changes: 53 additions & 13 deletions src/instance/properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
var properties = {
// set up property observers
observeProperties: function() {
//
// TODO(sjmiles):
// we observe published properties so we can reflect them to attributes
// ~100% of our team's applications would work without this reflection,
Expand Down Expand Up @@ -48,30 +49,66 @@
}
}
},
_observe: function(name, cb) {
log.observe && console.log(LOG_OBSERVE, this.localName, name);
registerObserver(this, name,
new PathObserver(this, name, cb));
},
observeAttributeProperty: function(name) {
var self = this;
// construct an observer on 'name' that ...
this._observe(name, function() {
// reflects the value to an attribute
self.relectPropertyToAttribute(name);
});
},
observeProperty: function(name, methodName) {
var self = this;
// construct an observer on 'name' that ...
this._observe(name, function(value, old) {
invoke.call(self, methodName, [old]);
// observes the value if it is an array
self.observeArrayValue(name, value, old);
// invokes user's side-effect method
self.invokeMethod(methodName, [old]);
});
},
observeBoth: function(name, methodName) {
var self = this;
// construct an observer on 'name' that ...
this._observe(name, function(value, old) {
// reflects the value to an attribute
self.relectPropertyToAttribute(name);
invoke.call(self, methodName, [old]);
// observes the value if it is an array
self.observeArrayValue(name, value, old);
// invokes user's side-effect method
self.invokeMethod(methodName, [old]);
});
},
observeArrayValue: function(name, value, old) {
// we only care if there are registered side-effects
var callbackName = this.observe[name];
if (callbackName) {
// if we are observing the previous value, stop
if (isArray(old)) {
log.observe && console.log('[%s] observeArrayValue: unregister observer [%s]', this.localName, name);
unregisterObserver(this, name + '__array');
}
// if the new value is an array, being observing it
if (isArray(value)) {
log.observe && console.log('[%s] observeArrayValue: register observer [%s]', this.localName, name, value);
var self = this;
var observer = new ArrayObserver(value, function(value, old) {
self.invokeMethod(callbackName, [old]);
});
registerObserver(this, name + '__array', observer);
}
}
},
_observe: function(name, cb) {
log.observe && console.log(LOG_OBSERVE, this.localName, name);
registerObserver(this, name, new PathObserver(this, name, cb));
// TODO(sjmiles): must use property descriptors otherwise we could
// be invoking a getter
var pd = Object.getOwnPropertyDescriptor(this.__proto__, name);
if (pd && pd.value) {
this.observeArrayValue(name, pd.value, null);
}
},
bindProperty: function(property, model, path) {
// apply Polymer two-way reference binding
return bindProperties(this, property, model, path);
Expand All @@ -81,15 +118,18 @@
},
unbindAllProperties: function() {
unregisterObservers(this);
},
invokeMethod: function(method, args) {
var fn = this[method] || method;
if (typeof fn === 'function') {
fn.apply(this, args);
}
}
};

function invoke(method, args) {
var fn = this[method] || method;
if (typeof fn === 'function') {
fn.apply(this, args);
}
}
function isArray(obj) {
return toString.call(obj) === "[object Array]";
};

// property binding

Expand Down

0 comments on commit 6004350

Please sign in to comment.