Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling controller action from view in Ember

Tags:

ember.js

I have a submit button with a onClick view event. This event checks a flag and depending upon the condition it will allow form submission. I'd like the submit action on the controller to be called. What is the best way to do this?

like image 333
bcardarella Avatar asked Feb 14 '13 06:02

bcardarella


1 Answers

Here another solution based on the example by albertjan for the case you have to perform some logic in your View and afterwards delegate to your controller. This is the way i understood your question:

HBS:

<script type="text/x-handlebars" data-template-name="index">
    <button {{action submit target="view"}} >Sumbit</button>
</script>

View:

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
    }
});

Controller:

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

Note: The call from your view will also bubble up to your current route. So this is basically the same code, that ember is executing when using the action helper.

like image 82
mavilein Avatar answered Sep 28 '22 08:09

mavilein