Skip to content

Instantly share code, notes, and snippets.

@tim-evans
Created September 21, 2011 18:58
Show Gist options
  • Select an option

  • Save tim-evans/1232987 to your computer and use it in GitHub Desktop.

Select an option

Save tim-evans/1232987 to your computer and use it in GitHub Desktop.
isSelected Binding
MO.SelectableFieldView = SC.View.extend({
content: null,
// ..........................................................
// VALIDATION
//
requiredFields: ['name'],
// Properties used for validation
nameBinding: '.content.name',
tagsBinding: '.content.tags',
sipAddressBinding: '.content.sipAddress',
xmppAddressBinding: '.content.xmppAddress',
isSelectedBinding: '.content.isSelected',
validate: function (property) {
var value = this.get(property);
return value != null && !/^\s*$/.test(value);
},
_wasEnabled: NO,
isValid: function () {
var isEnabled = this.get('requiredFields').every(this.validate, this) &&
['sipAddress', 'xmppAddress'].some(this.validate, this);
this.setPath('content.isEnabled', isEnabled);
if ((isEnabled && !this._wasEnabled) ||
(!isEnabled && this._wasEnabled)) {
this.set('isSelected', isEnabled);
}
this._wasEnabled = isEnabled;
return isEnabled;
}.property('name', 'tags', 'sipAddress', 'xmppAddress').cacheable(),
// ..........................................................
// VIEWS
//
childViews: ['checkbox'],
checkbox: SC.CheckboxView.design({
layout: { left: 0, top: 0, right: 0, bottom: 0 },
isEnabledBinding: SC.Binding.oneWay('.parentView.isValid').bool(),
isSelectedBinding: '.parentView.isSelected'
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment