我正在閱讀http://code418.com/blog/2012/03/26/advanced-emberjs-bindings/並且碰到了Ember .Binding.and用於變換,在當前的emberjs中已棄用Ember.computed。我決定更新舊的emberjs 0.9.x小提琴http://jsfiddle.net/Wjtcj/與emberjs 1.x一起工作,並提供了一個Ember.computed.and和新的小提琴http:// jsfiddle .net/Wjtcj/5/。雖然它的作品,我不能讓它返回thesame輸出作爲舊但當代碼http://jsfiddle.net/Wjtcj/28/的改進版本失敗Emberjs 1.x-pre Ember.Router和Ember.computed問題
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>
看來setSync函數是問題,並失敗,因爲我打電話給它的計算屬性。
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>
更新,請使用此鏈接,更新的代碼http://jsfiddle.net/Wjtcj/28/。下面沒有更多的代碼適用
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');
}
})
})
})
});
感謝您的任何建議或修正。
你'應用。 obj'永遠不會在最後一個jsfiddle中定義,這正是'不能調用undefined'的方法'setSync'' –
Trek非常感謝您的關注。雖然我沒有更新到最新的鏈接http://jsfiddle.net/Wjtcj/21/,但即使在將App.obj更新爲App.ObjController.setSync('isSelected',false );你可以看到我粘貼的代碼。只是還沒有更新鏈接。由於錯誤仍然可以,我可以做嗎?非常感謝。 – brg
當前鏈接** http://jsfiddle.net/Wjtcj/28/**。現在的問題是它開始進入路由器,並失敗與STATEMANAGER:輸入root.index –
brg