Created
November 14, 2012 01:34
-
-
Save mankind/4069679 to your computer and use it in GitHub Desktop.
Emberjs logical && + or
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
// You can run it here http://jsfiddle.net/Wjtcj/5/ | |
App = Ember.Application.create(); | |
App.and = Ember.computed.and = function(dependentKey, otherKey) { | |
return Ember.computed(dependentKey, otherKey, function(key) { | |
return get(this, dependentKey) && get(this, otherKey); | |
}); | |
}; | |
App.or = Ember.computed.or = function(dependentKey, otherKey) { | |
return Ember.computed(dependentKey, otherKey, function(key) { | |
return get(this, dependentKey) || get(this, otherKey); | |
}); | |
}; | |
App.obj = Ember.ArrayProxy.create({ | |
content: [], | |
user: Ember.Object.create({isAdmin: false, isOwner: false}), | |
isSelected: false, | |
isSaveEnabledBinding: App.and('user.isAdmin', 'isSelected'), | |
canReadBinding: App.or('user.isAdmin', 'user.isOwner'), | |
setSync: function(property, value) { | |
this.set(property, value); | |
Ember.run.sync(); // synchronize bindings | |
this.pushObject('isSaveEnabled = %@ ; canRead = %@'.fmt(this.get('isSaveEnabled'), this.get('canRead'))); | |
} | |
}); | |
App.obj.setSync('isSelected', false); | |
App.obj.setSync('user', Ember.Object.create({isAdmin: true, isOwner: false})); | |
App.obj.setSync('isSelected', true); | |
App.obj.setSync('user', Ember.Object.create({isAdmin: false, isOwner: true})); | |
App.obj.setSync('user', Ember.Object.create({isAdmin: false, isOwner: false})); | |
|
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
<script type="text/x-handlebars" > | |
{{#each App.obj}} | |
<p>{{this}}</p> | |
{{/each}} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment