Created
April 24, 2013 16:34
-
-
Save jedschneider/5453528 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Generated by CoffeeScript 1.3.3 | |
utensils.Bindable = (function() { | |
function Bindable(context, dataKey) { | |
if (context == null) { | |
context = $('body'); | |
} | |
this.dataKey = dataKey != null ? dataKey : 'bindable'; | |
this.bindables = $("[data-" + this.dataKey + "]", context); | |
this.instanceKey = "" + this.dataKey + "-instance"; | |
} | |
Bindable.prototype.bindAll = function() { | |
var el, _i, _len, _ref; | |
_ref = this.bindables; | |
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | |
el = _ref[_i]; | |
this.bind(el); | |
} | |
return this; | |
}; | |
Bindable.prototype.getRefs = function() { | |
var bindable, _i, _len, _ref, _results; | |
_ref = this.bindables; | |
_results = []; | |
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | |
bindable = _ref[_i]; | |
_results.push($(bindable).data(this.instanceKey)); | |
} | |
return _results; | |
}; | |
Bindable.prototype.dispose = function() { | |
var bindable, instance, _i, _len, _ref; | |
_ref = this.bindables; | |
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | |
bindable = _ref[_i]; | |
bindable = $(bindable); | |
if (instance = bindable.data(this.instanceKey)) { | |
if (typeof (instance != null ? instance.release : void 0) === 'function') { | |
instance.release(); | |
} | |
if (typeof (instance != null ? instance.dispose : void 0) === 'function') { | |
instance.dispose(); | |
} | |
bindable.data(this.instanceKey, null); | |
} | |
} | |
delete this.bindables; | |
this.bindables = []; | |
return this; | |
}; | |
Bindable.prototype.release = function() { | |
return this.dispose(); | |
}; | |
Bindable.prototype.bind = function(el, dataKey) { | |
var key, _class; | |
if (dataKey == null) { | |
dataKey = this.dataKey; | |
} | |
el = $(el); | |
key = el.data(dataKey); | |
if (_class = this.constructor.getClass(key)) { | |
if (!el.data(this.instanceKey)) { | |
return el.data(this.instanceKey, new _class(el)); | |
} | |
} else { | |
return typeof console !== "undefined" && console !== null ? console.error("Bindable for key: " + key + " not found in Bindable.registry for instance", el) : void 0; | |
} | |
}; | |
Bindable.getClass = function(key) { | |
var _ref; | |
return (_ref = this.registry[key]) != null ? _ref["class"] : void 0; | |
}; | |
Bindable.register = function(key, klass) { | |
var _ref; | |
if ((_ref = this.registry) == null) { | |
this.registry = {}; | |
} | |
this.registry[key] = { | |
"class": klass | |
}; | |
return null; | |
}; | |
return Bindable; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment