Ember Checkbox - 从不同的控制器访问模型属性

时间:2015-07-21 15:46:22

标签: ember.js checkbox

我在模式弹出窗口上有一个复选框,需要根据不相关模型上的值进行检查。该模型是Team,我需要访问的属性称为syncAccount(boolean),因此输入助手可能看起来像这样:

{{input type="checkbox" name="syncAccount" checked=team.syncAccount }}

如何从模式访问或绑定team.syncAccount?我有一个ModalImportController,但没有相关的路由。在这个控制器中是否有某种方法我可以为当前团队分配一个查找或绑定syncAccount值的属性(并在切换它时更新)?

同样,我需要切换复选框以发送此字段的更新请求。这需要是一个ajax请求,还是有一些方法可以设置复选框使用的模型指向一个团队,以便我可以调用@ model.save()?

1 个答案:

答案 0 :(得分:1)

要从其他控制器访问属性,首先需要通过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 }}
相关问题