Skip to content

Instantly share code, notes, and snippets.

@willbailey
Created April 1, 2009 23:28
Show Gist options
  • Save willbailey/88946 to your computer and use it in GitHub Desktop.
Save willbailey/88946 to your computer and use it in GitHub Desktop.
(function(){
Zen.UI.Mixins.Bindable = {
// bind the model to the view
setModel : function(model){
this.model = model;
this.model.observe(Observable.ALL_EVENTS, this._onModelEvent.bind(this), this, 'Zen.UI.View observer');
},
// handle binding to the model
_onModelEvent : function(e, item, set){
switch (e) {
case ZenItem.CHANGED:
var changedProperties = item.getChangedProperties();
Object.each(changedProperties, function(pair){
var property = pair.key;
var method = this[property.gsub(/_/,'-').camelize() + 'DidChange'];
var previousValue = item.getPreviousValue(property);
var newValue = item.get(property);
if (method) method(item, newValue, previousValue);
}.bind(this));
if (this.modelDidChange) this.modelDidChange(item);
break;
case ZenSet.ITEM_ADDED:
if (this.itemAddedToSet) this.setDidAddItem(item, set);
break;
case ZenSet.ITEM_REMOVED:
if (this.itemRemovedFromSet) this.setDidRemoveItem(item, set);
break;
};
},
// safely get a computed or data property from a model
getModelProperty : function(property){
if (!this.model) return null;
if (this.model.property) {
if (typeof(this.model.property) === 'function') return this.model.property();
return this.model.property;
}
return this.model.get(property);
},
setModelProperty : function(property, value, quiet){
if (!this.model) return null;
this.model.set(property, value, quiet);
}
};
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment