null checks for 'root'

This commit is contained in:
Scott Miles
2012-10-23 14:53:15 -07:00
parent ec5d1dfd85
commit e334f1293f

View File

@@ -16,8 +16,8 @@ license that can be found in the LICENSE file.
PROPERTY_CHANGED_SUFFIX: "Changed",
CUSTOM_EVENT_PREFIX: "at"
};
// polyfill for DOMTokenList features: list of classes in add/remove;
// polyfill for DOMTokenList features: list of classes in add/remove;
// enable method.
(function() {
'use strict';
@@ -37,24 +37,25 @@ license that can be found in the LICENSE file.
value ? this.add(name) : this.remove(name);
};
})();
(function(){
// attribute bindings
var bindAttrs = function(inAttributes) {
var attrs = this.boundAttributes = [];
if (inAttributes) {
var bindables = inAttributes.value.split(",");
bindables.forEach(function(a) {
attrs.push(a.trim());
a = a.trim();
attrs.push(a);
bindProperty.call(this, a, this[a]);
}, this);
}
};
// event bindings
var bindEvents = function(inEvents) {
if (inEvents) {
var bindables = inEvents.value.split(",");
@@ -69,7 +70,7 @@ license that can be found in the LICENSE file.
}, this);
}
};
// property bindings
var propertyChanged = function(inName, inOld) {
@@ -87,7 +88,7 @@ license that can be found in the LICENSE file.
}
};
};
var squelchSideEffects = false;
var setPropertySilently = function(inName, inValue) {
@@ -104,6 +105,7 @@ license that can be found in the LICENSE file.
// set default value in a property already bound via attrs
setPropertySilently.call(this, inName, value);
} else {
console.log('binding', inName, this)
var value = inValue;
var sideEffect = sideEffectFactory(inName);
Object.defineProperty(this, inName, {
@@ -120,7 +122,7 @@ license that can be found in the LICENSE file.
});
}
};
var bindProperties = function(inProperties) {
if (inProperties) {
Object.keys(inProperties).forEach(function(n) {
@@ -128,7 +130,7 @@ license that can be found in the LICENSE file.
}, this);
}
};
var deref = function(inNode) {
return inNode && (inNode.baby || inNode);
};
@@ -136,14 +138,16 @@ license that can be found in the LICENSE file.
var establishNodeReferences = function(inRoot) {
this.$ = this.$ || {};
// search the LOCAL tree
var nodes = ShadowDOM.localQueryAll(inRoot, "[id]");
Array.prototype.forEach.call(nodes, function(n) {
this.$[n.id] = deref(n);
}, this);
if (inRoot) {
var nodes = ShadowDOM.localQueryAll(inRoot, "[id]");
Array.prototype.forEach.call(nodes, function(n) {
this.$[n.id] = deref(n);
}, this);
}
};
// attribute mutations
var deserializeValue = function(inValue) {
switch (inValue) {
case "":
@@ -179,7 +183,7 @@ license that can be found in the LICENSE file.
propertyChanged.call(this, c.name, c.old);
}, this);
};
var attributeChanged = function(inName) {
var value = this.getAttribute(inName);
this[inName] = deserializeValue(value);
@@ -188,7 +192,7 @@ license that can be found in the LICENSE file.
var handleMutations = function(inMxns) {
inMxns.forEach(function(inMxn) {
var name = inMxn.attributeName;
if (this.boundAttributes[name]) {
if (this.boundAttributes.indexOf(name) >= 0) {
attributeChanged.call(this, name);
}
}, this);
@@ -220,7 +224,7 @@ license that can be found in the LICENSE file.
};
// decorate HTMLElementElement with toolkit API
HTMLElementElement.prototype.component = function(inUber) {
var attributes = this.element.attributes;
this.lifecycle({
@@ -246,11 +250,11 @@ license that can be found in the LICENSE file.
// install our prototype
this.generatedConstructor.prototype = p;
};
// utility methods
// job
var job = function(inJobName, inJob, inWait) {
var name = inJobName || ("__" + Math.random());
job.stop(name);
@@ -269,7 +273,7 @@ license that can be found in the LICENSE file.
job._jobs = {};
// target finding
var findDistributedTarget = function(inTarget, inItems) {
// find ancestor of target (including himself) that
// is in our item list, if any
@@ -282,19 +286,19 @@ license that can be found in the LICENSE file.
n = n.parentNode;
}
};
// collect utils
// collect utils
var utils = {
job: job,
findDistributedTarget: findDistributedTarget
};
// code below provides a shim for declarative event handlers
// (aka 'x', as in onclick="x('click')")
// it's only really for evaluating syntax, and not
// it's only really for evaluating syntax, and not
// a real solution
var nodeIterator = function(inNodes, inFn) {
if (inNodes) {
for (var i=0, n; (n=inNodes[i]); i++) {
@@ -335,7 +339,7 @@ license that can be found in the LICENSE file.
// experimental event handler for mapping events to component instances
// publish handler globally, make name as small as possible since
// this is ideally an invisible helper API
x = function(inHandler) {
var owner = findOwner(event.currentTarget);
//console.log(inHandler, owner, event);
@@ -343,7 +347,7 @@ license that can be found in the LICENSE file.
owner[inHandler](event);
}
};
// newer experimental event handler
var findController = function(inNode) {
@@ -356,7 +360,7 @@ license that can be found in the LICENSE file.
n = n.parentNode || n.host;
}
};
_ = function(inHandlerName) {
var controller = findController(event.currentTarget);
//console.log(inHandler, owner, event);
@@ -364,7 +368,7 @@ license that can be found in the LICENSE file.
controller[inHandlerName](event);
}
};
// automagic event mapping
var bindCustomEvent = function(inNode, inEventName, inHandler) {
@@ -388,7 +392,7 @@ license that can be found in the LICENSE file.
}
}
};
// TODO(sjmiles): improper tree walking (?)
var _bindAllCustomEvents = function(inNode) {
bindCustomEvents(inNode);
@@ -412,12 +416,14 @@ license that can be found in the LICENSE file.
subTree: true
});
};
var bindAllCustomEvents = function(inNode) {
_bindAllCustomEvents(inNode);
eventsObserver(inNode);
if (inNode) {
_bindAllCustomEvents(inNode);
eventsObserver(inNode);
}
};
})();
</script>
</element>