Ember.JS - Submit button runs a query and returns JSON object to template. Do I run the query in a service, as a model or in a route?

Viewed 50

I am still a novice and I am converting an application from Node Express to Ember I'm confused on some of the methodology involved in Ember.

In the current application there is a form that is submitted and then executes a query based on the input -sowing the results in a new template. In the app.js of the current application there is a switch statement on POST:

app.post('/test', function(req, res){
switch(req.body.module) {
    case "Option 1":
  getOption1Results(req, res);
    break;
    case "Option2":
  getOption2Results(req, res)
  break;
    case "Option 3":
  getOptio3Results(req, res)
  break;
  case "Option 4":
  getOption4Results(req, res)
  break;
  default:
    console.log("didn't work")

}

To get the same results in Ember I don't know whether I should be creating a service to do the above or creating a route and executing the query in a model that's passed to the template. Or if I need to handle it through an action from the form submit button.

Any advice is very much appreciated.

1 Answers

What i would do is the following:

  • Create a route to display the form to the user
  • Make an action on the form, such that when it is submitted, an action is called. Here you may validate the form, if you so desire
  • Make a service to execute the query (alternatively you could also use a controller, but i personally do not like them)

To call an action on form submit (both via submit button press, and enter key press), you could do something like:

<form {{action "onSubmitAction" on="submit"}}>
    {{input type="text" value="foobar"}}
    <button type="submit">Save</button>
</form>

And then in your controller (or component), you would define the following

actions: {
    onSubmitAction() {
        // Do something
    }
}
Related