Skip to content

Commit acf5ac1

Browse files
author
Scott Miles
committed
tweaks to improve data-binding
1 parent 0dd09a1 commit acf5ac1

File tree

1 file changed

+31
-27
lines changed

1 file changed

+31
-27
lines changed

src/g-component.html

+31-27
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<element name="g-component">
22
<script>
33

4-
// FOUC prevent (to opt-in set opacity: 0 on body)
4+
// FOUC prevent (to opt-in, set opacity: 0 on body)
55

66
window.addEventListener('WebComponentsReady', function() {
77
document.body.style.webkitTransition = "opacity 0.4s";
@@ -74,10 +74,10 @@
7474
function rootCreated(inRoot) {
7575
// set shadow-root model
7676
inRoot.model = this.$protected;
77-
// parse and apply model binding markup in DOM
78-
bindModel.call(this, localChildNodes(inRoot));
7977
// map id's to nodes, for nodes with id's
8078
marshalNodeReferences.call(this.$protected, inRoot);
79+
// parse and apply model binding markup in DOM
80+
bindModel.call(this, localChildNodes(inRoot));
8181
// accumulate events of interest
8282
// TODO(sjmiles): ideally on native we set up listeners directly on inRoot,
8383
// under shim inRoot does not participate in event bubbling
@@ -96,8 +96,8 @@
9696
takeAttributes.call(this.$protected);
9797
// call ready hook
9898
this.$protected.ready();
99-
// force dirty check
100-
Model.dirtyCheck();
99+
// TODO(sjmiles): explain succinctly why this dirtyCheck is here
100+
window.dirtyCheck();
101101
log.ready && console.log('[%s#%s] ready', this.localName, this.id || '');
102102
};
103103

@@ -302,9 +302,9 @@
302302
},
303303
propertySetChanged: function(inChange, inWatched) {
304304
if (inChange.mutation == 'add') {
305-
log.data && console.log('[%s] propertySetChanged: adding watch for [%s]', this.node.localName, inChange.propertyName);
305+
//log.watch && console.log('[%s] propertySetChanged: adding watch for [%s]', this.node.localName, inChange.propertyName);
306306
api.addWatch.call(this, inChange.propertyName, inWatched);
307-
this.propertyChanged(inChange.propertyName);
307+
//this.propertyChanged(inChange.propertyName);
308308
}
309309
},
310310
watchable: function(inName, inWatched) {
@@ -317,9 +317,10 @@
317317
},
318318
addWatch: function(inName, inWatched) {
319319
if (api.watchable.call(this, inName, inWatched)) {
320-
log.data && console.log('[' + this.node.localName + '] watching [' + inName + ']');
320+
log.watch && console.log('[' + this.node.localName + '] watching [' + inName + ']');
321321
inWatched[inName] = true;
322322
Model.observe(this, inName, function(inNew, inOld) {
323+
log.data && console.log('[%s#%s] watch: [%s] now [%s] was [%s]', this.node.localName, this.node.id || '', inName, this[inName], inOld);
323324
this.propertyChanged(inName, inOld);
324325
}.bind(this));
325326
}
@@ -333,7 +334,7 @@
333334

334335
Observable.prototype = {
335336
propertyChanged: function(inName, inOldValue) {
336-
log.data && console.log('[%s#%s] propertyChanged: [%s] now [%s] was [%s]', this.node.localName, this.node.id || '', inName, this[inName], inOldValue);
337+
//log.data && console.log('[%s#%s] propertyChanged: [%s] now [%s] was [%s]', this.node.localName, this.node.id || '', inName, this[inName], inOldValue);
337338
var fn = inName + 'Changed';
338339
if (this[fn] && !Observable._squelch) {
339340
this[fn](inOldValue);
@@ -533,9 +534,9 @@
533534
// nested template and functions asynchronously
534535

535536
function bindModel(inNodes) {
536-
log.data && console.group("[%s] bindModel", this.localName);
537+
log.bind && console.group("[%s] bindModel", this.localName);
537538
_bindModel.call(this, inNodes);
538-
log.data && console.groupEnd();
539+
log.bind && console.groupEnd();
539540
};
540541

541542
function _bindModel(inNodes) {
@@ -575,7 +576,7 @@
575576
} else {
576577
if ((inValue || inName).search(bindMustache.pattern) >= 0) {
577578
inNode.addBinding(inName, inValue);
578-
log.data && console.log('[%s] bindMustache: [%s] to [%s]%s',
579+
log.bind && console.log('[%s] bindMustache: [%s] to [%s]%s',
579580
this.localName, inValue || inName, (inNode.localName || '#text'),
580581
inValue ? '.[' + inValue + ']' : '');
581582
}
@@ -587,8 +588,10 @@
587588
// property binding extensions to MDV
588589

589590
function bindProperties(inA, inPropA, inB, inPropB) {
590-
log.data && console.log('[%s] bindProperties [%s] to [%s].[%s]', inA.localName, inPropA, inB.localName, inPropB);
591+
log.bind && console.log('[%s] bindProperties [%s] to [%s].[%s]', inA.localName, inPropA, inB.localName, inPropB);
591592
_bindProperties(inA.$protected, inPropA, inB.$protected, inPropB);
593+
// TODO(sjmiles): explain succinctly why this dirtyCheck is here
594+
window.dirtyCheck();
592595
};
593596

594597
function _getProp(parts, create, context) {
@@ -610,28 +613,29 @@
610613
function getProperty(name, create, context) {
611614
return _getProp(name.split("."), create, context);
612615
};
613-
614-
616+
615617
function _bindProperties(inA, inPropA, inB, inPropB) {
616-
setProperty(inPropB, getProperty(inPropA, false, inA), inB);
618+
var value = getProperty(inPropA, true, inA);
619+
log.data && console.log('[%s] _bindProperties: sending [%s] (%s) to [%s].[%s]', inA.node.localName, inPropA, value, inB.node.localName, inPropB);
620+
setProperty(inPropB, value, inB);
621+
bindProperty(inA, inPropA, inB, inPropB);
622+
bindProperty(inB, inPropB, inA, inPropA);
623+
};
624+
625+
function bindProperty(inA, inPropA, inB, inPropB) {
617626
Model.observe(inA, inPropA, function(inNew) {
618627
// intermediate changes are observed, but are irrelevant
619628
// we only propagate when the value stabilizes
620629
if (getProperty(inPropA, false, inA) === inNew) {
621-
log.data && console.log('_bindProperties: propagating', inA.node.localName, inPropA, '(' + inNew + ')', 'to', inB.node.localName, inPropB);
622-
setProperty(inPropB, inNew, inB);
623-
}
624-
});
625-
Model.observe(inB, inPropB, function(inNew) {
626-
// intermediate changes are observed, but are irrelevant
627-
// we only propagate when the value stabilizes
628-
if (getProperty(inPropB, false, inB) === inNew) {
629-
log.data && console.log('_bindProperties: propagating', inB.node.localName, inPropB, '(' + inNew + ')', 'to', inA.node.localName, inPropA);
630-
setProperty(inPropA, inNew, inA);
630+
// probably redundant, but prevent circularity right here
631+
if (getProperty(inPropB, false, inB) !== inNew) {
632+
log.data && console.log('[%s] bindProperty: propagating [%s] (%s) to [%s].[%s]', inA.node.localName, inPropA, inNew, inB.node.localName, inPropB);
633+
setProperty(inPropB, inNew, inB);
634+
}
631635
}
632636
});
633637
};
634-
638+
635639
//
636640
// attribute processing
637641
//

0 commit comments

Comments
 (0)