Skip to content
This repository has been archived by the owner on Mar 13, 2018. It is now read-only.

Commit

Permalink
update to reflect changes to observe-js/Node.bind
Browse files Browse the repository at this point in the history
 - bind changed to take an observable
 - CompoundPathObserver changed to CompoundObserver.
  • Loading branch information
sorvell committed Dec 13, 2013
1 parent 9245e2d commit c94c872
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 42 deletions.
6 changes: 3 additions & 3 deletions src/instance/mdv.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
template.bindingDelegate = this.syntax;
return template.createInstance(this);
},
bind: function(name, model, path) {
bind: function(name, observable) {
// note: binding is a prepare signal. This allows us to be sure that any
// property changes that occur as a result of binding will be observed.
if (!this._elementPrepared) {
Expand All @@ -45,9 +45,9 @@
// clean out the closets
this.unbind(name);
// use n-way Polymer binding
var observer = this.bindProperty(property, model, path);
var observer = this.bindProperty(property, observable);
// stick path on observer so it's available via this.bindings
observer.path = path;
observer.path = observable.path_;
// reflect bound property to attribute when binding
// to ensure binding is not left on attribute if property
// does not update due to not changing.
Expand Down
62 changes: 27 additions & 35 deletions src/instance/properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
var n$ = this._observeNames, pn$ = this._publishNames;
if ((n$ && n$.length) || (pn$ && pn$.length)) {
var self = this;
var o = this._propertyObserver = generateCompoundPathObserver(this);
var o = this._propertyObserver = new CompoundObserver();
for (var i=0, l=n$.length, n; (i<l) && (n=n$[i]); i++) {
o.addPath(this, n);
// observer array properties
Expand All @@ -36,26 +36,24 @@
o.addPath(this, n);
}
}
o.start();
o.open(this.notifyPropertyChanges, this);
}
},
notifyPropertyChanges: function(newValues, oldValues, changedBits, paths) {
var called = {};
for (var i=0, l=changedBits.length, name, method; i<l; i++) {
if (changedBits[i]) {
// note: paths is of form [object, path, object, path]
name = paths[2 * i + 1];
if (this.publish[name] !== undefined) {
this.reflectPropertyToAttribute(name);
}
method = this.observe[name];
if (method) {
this.observeArrayValue(name, newValues[i], oldValues[i]);
if (!called[method]) {
called[method] = true;
// observes the value if it is an array
this.invokeMethod(method, [oldValues[i], newValues[i], arguments]);
}
notifyPropertyChanges: function(newValues, oldValues, paths) {
var name, method, called = {};
for (var i in oldValues) {
// note: paths is of form [object, path, object, path]
name = paths[2 * i + 1];
if (this.publish[name] !== undefined) {
this.reflectPropertyToAttribute(name);
}
method = this.observe[name];
if (method) {
this.observeArrayValue(name, newValues[i], oldValues[i]);
if (!called[method]) {
called[method] = true;
// observes the value if it is an array
this.invokeMethod(method, [oldValues[i], newValues[i], arguments]);
}
}
}
Expand All @@ -72,17 +70,17 @@
// if the new value is an array, being observing it
if (Array.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]);
});
var observer = new ArrayObserver(value);
observer.open(function(value, old) {
this.invokeMethod(callbackName, [old]);
}, this);
this.registerObserver(name + '__array', observer);
}
}
},
bindProperty: function(property, model, path) {
bindProperty: function(property, observable) {
// apply Polymer two-way reference binding
return bindProperties(this, property, model, path);
return bindProperties(this, property, observable);
},
unbindAllProperties: function() {
if (this._propertyObserver) {
Expand Down Expand Up @@ -124,24 +122,18 @@
}
};

// compound path observer
function generateCompoundPathObserver(element) {
return new CompoundPathObserver(element.notifyPropertyChanges, element);
}

// property binding
// bind a property in A to a path in B by converting A[property] to a
// getter/setter pair that accesses B[...path...]
function bindProperties(inA, inProperty, inB, inPath) {
function bindProperties(inA, inProperty, observable) {
log.bind && console.log(LOG_BIND_PROPS, inB.localName || 'object', inPath, inA.localName, inProperty);
// capture A's value if B's value is null or undefined,
// otherwise use B's value
var path = Path.get(inPath);
var v = path.getValueFrom(inB);
var v = observable.getValue();
if (v === null || v === undefined) {
path.setValueFrom(inB, inA[inProperty]);
observable.setValue(inA[inProperty]);
}
return PathObserver.defineProperty(inA, inProperty, inB, inPath);
return Observer.defineProperty(inA, inProperty, observable);
}

// logging
Expand Down
11 changes: 7 additions & 4 deletions test/js/bindProperties.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ suite('bindProperties', function() {
test('bind properties getter', function() {
var a = {};
var b = {bar: 1};
Polymer.api.instance.properties.bindProperty.call(a, 'foo', b, 'bar');
var observable = new PathObserver(b, 'bar');
Polymer.api.instance.properties.bindProperty.call(a, 'foo', observable);
assert.equal(a.foo, 1);
b.bar = 5;
assert.equal(a.foo, 5);
});

test('bind properties setter', function() {
var a = {};
var b = {bar: 1};
Polymer.api.instance.properties.bindProperty.call(a, 'foo', b, 'bar');
var observable = new PathObserver(b, 'bar');
Polymer.api.instance.properties.bindProperty.call(a, 'foo', observable);
assert.equal(b.bar, 1);
a.foo = 5;
assert.equal(b.bar, 5);
Expand All @@ -28,7 +30,8 @@ suite('bindProperties', function() {
test('bind properties paths', function() {
var a = {};
var b = {bar: {zot: 2}};
Polymer.api.instance.properties.bindProperty.call(a, 'foo', b, 'bar.zot');
var observable = new PathObserver(b, 'bar.zot');
Polymer.api.instance.properties.bindProperty.call(a, 'foo', observable);
assert.equal(a.foo, 2);
b.bar.zot = 9;
assert.equal(a.foo, 9);
Expand Down

0 comments on commit c94c872

Please sign in to comment.