从Ember中的视图调用控制器操作

bca*_*lla 10 ember.js

我有一个带有onClick视图事件的提交按钮.此事件检查标志,并根据条件允许表单提交.我想submit调用控制器上的动作.做这个的最好方式是什么?

mav*_*ein 23

这里有另一个基于albertjan示例的解决方案,您必须在View中执行某些逻辑,然后再委托给您的控制器.这是我理解你的问题的方式:

HBS:

<script type="text/x-handlebars" data-template-name="index">
    <button {{action submit target="view"}} >Sumbit</button>
</script>
Run Code Online (Sandbox Code Playgroud)

视图:

App.ThingView = Ember.View.extend({
    submit : function(){
            //do the view part of your logic
        var object = //do whatever you may need
        this.get("controller").send("submitInController", object); //you do not have to send object, if you do not need to
    }
});
Run Code Online (Sandbox Code Playgroud)

控制器:

App.ThingController = Em.ObjectController.extend({
    submitInController: function(model) {
        // do the controller part of your logic
    }
});  
Run Code Online (Sandbox Code Playgroud)

注意:您视图中的来电也会冒泡到您当前的路线.所以这基本上是相同的代码,ember在使用动作助手时正在执行.