I was reading http://code418.com/blog/2012/03/26/advanced-emberjs-bindings/ and came across Ember.Binding.and for transform which has deprecated in the current emberjs for Ember.computed. I decided to update the old emberjs 0.9.x fiddle http://jsfiddle.net/Wjtcj/ to work with emberjs 1.x and provided an Ember.computed.and as shown in the new fiddle http://jsfiddle.net/Wjtcj/5/. Though it works, i cant make it return thesame output as the old one but when an improved version of the code http://jsfiddle.net/Wjtcj/28/ fails with
STATEMANAGER: Sending event 'navigateAway' to state root.
STATEMANAGER: Sending event 'unroutePath' to state root.
STATEMANAGER: Sending event 'routePath' to state root.
STATEMANAGER: Entering root.index
<error>
It seems the setSync function is the issue and fails because i am calling computed property on it.
The handlebars template:
<script type="text/x-handlebars" data-template-name="application" >
{{outlet}}
</script>
<script type="text/x-handlebars" data-template-name="obj" >
{{#each App.ObjController}}
<p>{{this}}</p>
{{/each}}
</script>
update, please use this link for the updated code http://jsfiddle.net/Wjtcj/28/. The code below no more applies
App = Ember.Application.create();
Ember.computed.and = function(dependentKey, otherKey) {
return Ember.computed(dependentKey, otherKey, function(key) {
return get(this, dependentKey) && get(this, otherKey);
});
};
Ember.computed.or = function(dependentKey, otherKey) {
return Ember.computed(dependentKey, otherKey, function(key) {
return get(this, dependentKey) || get(this, otherKey);
});
};
App.ApplicationController = Em.Controller.extend();
App.ApplicationView = Ember.View.extend({
templateName: 'application'
});
App.ObjView = Em.View.extend({
templateName: 'obj'
});
App.ObjController = Ember.ArrayController.extend({
content: [],
user: Ember.Object.create({isAdmin: false, isOwner: false}),
isSelected: false,
isSaveEnabled: false,
canRead: false,
isSaveEnabledBinding: Ember.computed.and('user.isAdmin', 'isSelected'),
canReadBinding: Ember.computed.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.ObjController.setSync('isSelected', false);
App.ObjController.setSync('user', Ember.Object.create({isAdmin: true, isOwner: false}));
App.ObjController.setSync('isSelected', true);
App.ObjController.setSync('user', Ember.Object.create({isAdmin: false, isOwner: true}));
App.ObjController.setSync('user', Ember.Object.create({isAdmin: false, isOwner: false}));
App.Router = Ember.Router.extend({
enableLogging: true,
location: 'hash',
root: Ember.Route.extend({
index: Ember.Route.extend({
route: '/',
connectOutlets: function(router) {
router.get('applicationController').connectOutlet('application');
}
}),
obj: Ember.Route.extend({
route: '/obj',
enter: function(router) {
console.log("The obj sub-state was entered.");
},
index: Ember.Route.extend({
route: '/',
connectOutlets: function(router, context) {
router.get('applicationController').connectOutlet( 'obj');
}
})
})
})
});
Thanks for any suggestions or fix.
App.obj
is never defined in the last jsfiddle., which is exactly what 'Cannot call method 'setSync' of undefined' – Unconventional