+ + +
diff --git a/bindable_modern.js b/bindable_modern.js new file mode 100644 index 0000000..f09161c --- /dev/null +++ b/bindable_modern.js @@ -0,0 +1,74 @@ +window.Bindable = (function() { + + function Bindable(context, dataKey) { + context = context || 'body' + this.dataKey = dataKey || 'data-bindable' + this.instanceKey = this.dataKey.replace(/data-/g, '') + 'Instance' + this.bindables = document[context].querySelectorAll('[' + this.dataKey + ']') + } + + + Bindable.prototype.bindAll = function() { + for (var i = 0, len = this.bindables.length; i < len; i += 1) { + this.bind(this.bindables[i]) + } + return this + }; + + + Bindable.prototype.getRefs = function() { + var refs = [] + for (var i = 0, len = this.bindables.length; i < len; i += 1) { + refs.push(this.bindables[i][this.instanceKey]) + } + return refs + }; + + + Bindable.prototype.dispose = function() { + var instance + for (var i = 0, len = this.bindables.length; i < len; i += 1) { + var bindable = this.bindables[i] + if (instance = bindable[this.instanceKey]) { + if (typeof (instance != null ? instance.dispose : void 0) === 'function') { + instance.dispose() + } + bindable[this.instanceKey] = null + } + } + this.bindables = [] + return this + }; + + + Bindable.prototype.bind = function(el, dataKey) { + dataKey = dataKey || this.dataKey + var _class + var key = el.getAttribute(dataKey) + if (_class = this.constructor.getClass(key)) { + if (!el[this.instanceKey]) { + el[this.instanceKey] = new _class(el) + } + } else { + throw new Error('Bindable for key: ' + key + ' not found in Bindable.registry for instance ' + el) + } + }; + + + Bindable.getClass = function(key) { + key = '"' + key + '"' + return (this.registry[key] ? this.registry[key]['class'] : void 0) + }; + + + Bindable.register = function(key, klass) { + this.registry = this.registry || {} + this.registry['"' + key + '"'] = {'class': klass} + return this.registry + }; + + + return Bindable + +})(); + diff --git a/index.html b/index.html new file mode 100644 index 0000000..cb0f45d --- /dev/null +++ b/index.html @@ -0,0 +1,26 @@ + + +
+ +
+ Simple Binder Constructor
"
+ }
+
+ SimpleBinder.prototype.dispose = function() {
+ this.el.innerHTML += "Simple Binder Disposed
"
+ }
+
+ SimpleBinder.prototype.likeAString = function() {
+ return "SimpleBinder\n"
+ };
+
+ return SimpleBinder;
+
+})();
+
+Bindable.register('simple-binder', window.SimpleBinder);
+
+
+(function() {
+ var bindable = new Bindable().bindAll();
+ var refs = bindable.getRefs()
+ var code = document.querySelector('.refs')
+
+ for (var i = 0, len = refs.length; i < len; i += 1) {
+ code.innerHTML += "Ref " + i + ": " + refs[i].likeAString()
+ }
+ console.log(refs)
+ bindable.dispose()
+}())
+