Ember复选框 - 从不同控制器访问模型属性
问题描述:
我有一个模式弹出框的复选框,需要根据无关模型上的值进行检查。该模型是和我的团队需要访问被称为syncAccount(布尔)属性,所以输入助手可能会是这个样子:Ember复选框 - 从不同控制器访问模型属性
{{input type="checkbox" name="syncAccount" checked=team.syncAccount }}
如何从模态访问或绑定到team.syncAccount?我有一个ModalImportController,但没有关联的路线。有没有在这个控制器中的一些方法,我可以分配一个属性,查找或绑定到当前团队的syncAccount的值(并在切换时更新)?
同样,我需要切换复选框发送此字段的更新请求。这需要是ajax请求,还是有一些方法可以将复选框使用的模型设置为指向团队,以便我可以调用@ model.save()?
答
要访问从另一个控制器属性首先需要包括通过needs
像这样:
App.ModalImportController = Ember.ArrayController.extend({
needs: "team",
teamController: Ember.computed.alias("controllers.team"),
});
,那么你将有机会获得它,像这样的属性:
// still in ModalImportController
syncAccount: function() {
return this.get('teamController.syncAccount');
}.property('teamController.syncAccount')
我现在还没有对它进行测试,但这是我在稍微不同的设置中完成的。
来源: [http://guides.emberjs.com/v1.13.0/controllers/dependencies-between-controllers/][1]
对于toggeling送我用的更新请求:
syncAccount: Ember.computed('model.syncAccount', {
get: function() {
return this.get('model.syncAccount');
},
set: function(key, value) {
this.set('model.syncAccount', value);
// do all you need to do via AJAX here
return value;
}
})
纸条,那你也可以得到从这里的价值,所以改变你的输入帮手:
{{input type="checkbox" name="syncAccount" value=syncAccount }}
我没有团队控制员。相反,在应用程序控制器中,我有一个名为currentTeam的助手。我使用你的建议来访问像这样的值:'syncAccount:function(){this.get('applicationController.currentTeam.syncAccount'); } .property('currentTeam.syncAccount')'syncAccount的值似乎没有得到最初的查询。从开发工具中,如果我调用$ E.syncAccount,它会返回:function()App.ModalsController.Ember.ObjectController.extend.syncAccount()切换复选框后,调用$ E.syncAccount返回'true'。如何在打开前设置此值? – Jim
看起来好像是输入助手有问题: value = syncAccount不起作用,但value =“syncAccount”将复选框标记为首次打开时选中。切换仍然不起作用(即,单击复选框不会在检查/不检查之间切换。是否碰巧知道这是我必须在控制器中处理的行为(即不是默认值?) – Jim