Created
April 1, 2009 23:28
-
-
Save willbailey/88946 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
(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