Created
January 24, 2011 11:47
-
-
Save kuon/793120 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
SG.SelectView = SC.View.extend({ | |
displayProperties:['items', 'itemTitleKey', 'itemValueKey'], | |
tagName:"select", | |
render:function(ctx, first) { | |
ctx.addClass('sg-select'); | |
if(!this.get('isEnabled')) { | |
ctx.attr('disabled','disabled'); | |
ctx.addClass('disabled'); | |
return; | |
} | |
var items = this.get('items'); | |
if(!items) return; | |
var itemValueKey = this.get('itemValueKey'); | |
var itemTitleKey = this.get('itemTitleKey'); | |
this._firstValue = null; | |
items.forEach(function(item) { | |
var value = GN.serializeObject(item, itemValueKey); | |
var title = GN.serializeObject(item, itemTitleKey); | |
this._firstValue = this._firstValue || value; | |
ctx.push('<option value="'+value+'">'+title+'</option>'); | |
}, this); | |
}, | |
_valueDidChange:function() { | |
if(this._updatingValue || !this.get('isEnabled')) return; | |
var value = this.get('value'); | |
this._updatingValue = YES; | |
SC.RunLoop.begin(); | |
if(!value) { | |
this.setIfChanged('value', this._firstValue); | |
value = this.get('value'); | |
} | |
this.$().val(value); | |
SC.RunLoop.end(); | |
this._updatingValue = NO; | |
}.observes('value'), | |
_fieldValueDidChange:function() { | |
this.setIfChanged('value', this.$().val()); | |
}, | |
_syncFirstValue:function() { | |
var value = this.get('value'); | |
if(!value) { | |
this.setIfChanged('value', this._firstValue); | |
} | |
}, | |
didCreateLayer: function() { | |
sc_super(); | |
SC.Event.add(this.$(), 'change', this, '_fieldValueDidChange') ; | |
}, | |
willDestroyLayer: function() { | |
SC.Event.remove(this.$(), 'change', this, '_fieldValueDidChange'); | |
}, | |
_itemsDidChange:function() { | |
this.set('layerNeedsUpdate', YES); | |
}.observes('*items.[]') | |
}); |
Author
kuon
commented
Jan 24, 2011
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment