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

Commit

Permalink
Property bindings now use accessors on the element prototype rather t…
Browse files Browse the repository at this point in the history
…han the instance. This is a performance improvement.
  • Loading branch information
Steve Orvell committed Apr 11, 2014
1 parent ad962bc commit dccc3fe
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 21 deletions.
14 changes: 14 additions & 0 deletions src/declaration/properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,21 @@
map[n.toLowerCase()] = n;
}
return map;
},
createPropertyAccessors: function(prototype) {
var n$ = prototype._observeNames, pn$ = prototype._publishNames;
if ((n$ && n$.length) || (pn$ && pn$.length)) {
for (var i=0, l=n$.length, n; (i<l) && (n=n$[i]); i++) {
Observer.createBindablePrototypeAccessor(prototype, n);
}
for (var i=0, l=pn$.length, n; (i<l) && (n=pn$[i]); i++) {
if (!prototype.observe || (prototype.observe[n] === undefined)) {
Observer.createBindablePrototypeAccessor(prototype, n);
}
}
}
}

};

// exports
Expand Down
1 change: 1 addition & 0 deletions src/declaration/prototype.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
desugarAfterChaining: function(name, extendee) {
// build side-chained lists to optimize iterations
this.optimizePropertyMaps(this.prototype);
this.createPropertyAccessors(this.prototype);
// install external stylesheets as if they are inline
this.installSheets();
// adjust any paths in dom from imports
Expand Down
6 changes: 3 additions & 3 deletions src/instance/properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@
}
},
bindProperty: function(property, observable) {
// apply Polymer two-way reference binding
return bindProperties(this, property, observable);
},
invokeMethod: function(method, args) {
Expand Down Expand Up @@ -145,7 +144,8 @@
if (v === null || v === undefined) {
observable.setValue(inA[inProperty]);
}
return Observer.defineComputedProperty(inA, inProperty, observable);
// apply Polymer two-way reference binding
return Observer.bindToInstance(inA, inProperty, observable);
}

// logging
Expand All @@ -158,4 +158,4 @@

scope.api.instance.properties = properties;

})(Polymer);
})(Polymer);
34 changes: 17 additions & 17 deletions test/html/prop-attr-bind-reflection.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ <h1>Hello from the child</h1>
<p>The lowercase is {{lowercase}}, attr is {{attributes.lowercase.value}}</p>
</template>
<script>
Polymer('my-child-element', {
publish: { camelCase: 0, lowercase: 0 },
// Make this a no-op, so we can verify the initial
// reflectPropertyToAttribute works.
observeAttributeProperty: function(name) { }
});
Polymer('my-child-element', {
publish: { camelCase: 0, lowercase: 0 },
// Make this a no-op, so we can verify the initial
// reflectPropertyToAttribute works.
observeAttributeProperty: function(name) { }
});
</script>
</polymer-element>

Expand All @@ -34,19 +34,19 @@ <h1>Hello from the custom element. The volume is {{volume}}</h1>
</p>
</template>
<script>
Polymer('my-element', {
publish: { volume: 11 },
ready: function() {
var child = this.$.child;
chai.assert.equal(child.lowercase, 11);
chai.assert.equal(child.camelCase, 11);
Polymer('my-element', {
publish: { volume: 11 },
ready: function() {
var child = this.$.child;
chai.assert.equal(child.lowercase, 11);
chai.assert.equal(child.camelCase, 11);

chai.assert.equal('' + child.lowercase, child.getAttribute('lowercase'));
chai.assert.equal('' + child.camelCase, child.getAttribute('camelcase'));
chai.assert.equal('' + child.lowercase, child.getAttribute('lowercase'));
chai.assert.equal('' + child.camelCase, child.getAttribute('camelcase'));

done();
}
});
done();
}
});
</script>
</polymer-element>

Expand Down
3 changes: 2 additions & 1 deletion test/html/property-array-changes.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
(function() {
var a = [], a1 = [];
Polymer('x-test', {
b: [],
ready: function() {
this.a = a;
},
Expand All @@ -33,6 +32,7 @@
},
nextTest: function() {
var m = this.getCurrentTest();
//console.log(this.currentTest);
if (m) {
m.call(this);
}
Expand Down Expand Up @@ -73,6 +73,7 @@
},
test6: function() {
chai.assert.equal(this.a.length, 1, 'old array properly unobserved');
this.b = [];
this.b.push(1);
},
test7: function() {
Expand Down
3 changes: 3 additions & 0 deletions test/js/bindProperties.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ suite('bindProperties', function() {
var a = {};
var b = {bar: 1};
var observable = new PathObserver(b, 'bar');
Observer.createBindablePrototypeAccessor(a, 'foo');
Polymer.api.instance.properties.bindProperty.call(a, 'foo', observable);
assert.equal(a.foo, 1);
b.bar = 5;
Expand All @@ -21,6 +22,7 @@ suite('bindProperties', function() {
var a = {};
var b = {bar: 1};
var observable = new PathObserver(b, 'bar');
Observer.createBindablePrototypeAccessor(a, 'foo');
Polymer.api.instance.properties.bindProperty.call(a, 'foo', observable);
assert.equal(b.bar, 1);
a.foo = 5;
Expand All @@ -31,6 +33,7 @@ suite('bindProperties', function() {
var a = {};
var b = {bar: {zot: 2}};
var observable = new PathObserver(b, 'bar.zot');
Observer.createBindablePrototypeAccessor(a, 'foo');
Polymer.api.instance.properties.bindProperty.call(a, 'foo', observable);
assert.equal(a.foo, 2);
b.bar.zot = 9;
Expand Down

0 comments on commit dccc3fe

Please sign in to comment.