简体   繁体   English

Ember.js将动作发送到“ belongsTo”关联模型的控制器

[英]Ember.js send action to controller of a 'belongsTo' associated model

I have a "children" template like this which is rendered with an array of "child" records: 我有一个像这样的“儿童”模板,该模板以“儿童”记录数组呈现:

{{#each}}
    {{render "parent.edit" parent}}
{{/each}}
<button {{action="submit"}}>Submit</button>

And a "parent.edit" template like this: 还有一个“ parent.edit”模板,如下所示:

{{input type="text" value=parentProp}}

Basically what I want to have happen is that when the submit button is clicked, each "child" tells its parent model to save itself by sending an action to a ParentEditController, which does some stuff based on controller state and then triggers a model save (whose property is bound to the text input). 基本上,我想发生的事情是,当单击“提交”按钮时,每个“子”都通过将操作发送到ParentEditController来告诉其父模型保存自身,该操作根据控制器状态执行一些操作,然后触发模型保存(其属性绑定到文本输入)。

For the controllers and models I have: 对于控制器和模型,我有:

App.Child = DS.model.extend({
    parent: DS.belongsTo('parent', {async: true})
});

App.Parent = DS.model.extend({
    parentProp: DS.attr('string')
});

App.ChildrenController = Ember.ArrayController.extend({
    itemController: 'child',
    actions: {
        submit: function() {
            //How can I send 'save' to each child.parent?
            this.forEach(function(item) {
                item.send('saveParent'); //Calls ChildController, which has a model
            });
        }
    }
});

App.ChildItemController = Ember.ObjectController.extend({
    needs: ['parentEdit'],
    actions: {
        saveParent: function() {
            //Here is the problem.  How do I get a non-singleton instance (has a model) of ParentItemController?
            this.get('controllers.parentEdit').send('doStuffThenSave'); //This sends to a singleton instance of the controller without a model.
            this.get('parent').send('doStuffThenSave'); //This sends to the model directly and does not hit any controller
        }
    }
});

App.ParentEditController = Ember.ObejctController.extend({
    actions: {
        doStuffThenSave: function() {
            //Here there is some code to do some stuff that is dependent on other controller properties before I save the model.
            this.get('model').save(); //should update 'parentProp' based on the input
        }
    }
});

As in the code comments, the issue is that if I try to reference ParentEditController with 'needs', then I get a singleton instance because the controller is set up with the model in the line {{render "parentItem" parent}} . 如代码注释中所述,问题在于,如果我尝试使用“需要”引用ParentEditController,则将得到一个单例实例,因为控制器是通过{{render "parentItem" parent}}行中的模型设置的。 ie, the way I understand it, 'needs' only gives you a controller with a model when the controller has been set up in the route or in a parent template somewhere. 即,按照我的理解,仅当在路径或父模板中的某个位置设置了控制器时,“需要”才为您提供具有模型的控制器。 If I go straight to the model, then I don't have access to the controller. 如果我直接进入模型,则无法访问控制器。 How can I have both? 我怎么都可以?

In App.ChildItemController you should change 在App.ChildItemController中,您应该更改

this.get('controllers.parentEdit').send('doStuffThenSave'); 
this.get('parent').send('doStuffThenSave');

to

this.get('controllers.parentEdit').send('doStuffThenSave', this.get('model').get('parent'));

and

doStuffThenSave: function() {...}

to

doStuffThenSave: function(<param-name>) {...}

since you aren't sending any context to the ParentEditController and no model is defined, then it is using a proxy model with no info. 由于您没有将任何上下文发送到ParentEditController且未定义任何模型,因此它正在使用没有信息的代理模型。 By sending the context and defining a param you are sending context(in this case a parent model of the child item) for parentEdit.doStuffThenSave to work with. 通过发送上下文并定义参数,您正在发送上下文(在本例中为子项的父模型)以供parentEdit.doStuffThenSave使用。

See Ember.Controller.send 参见Ember.Controller.send

You will also need to change 您还需要更改

this.get('model').save();

to

<param-name>.save();

side point, don't forget to handle error/success with then() after the save! 一点,不要忘记在保存后使用then()处理错误/成功!

Guide on emberjs.com emberjs.com上的指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM