3
votes

my question is a little bit general. What is the best concept for route and controller with findQuery in ember.

I have api with data filtering. Data request is executed by

this.store.findQuery('dataModel', {"q": JSON.stringify({"filters": filters})});

after that I show them in table view. The filter is updated by form views in a template.

My current solution: Form views set controller parameters and a button call action from controller. Controller action loads parameter, executes findQuery and set('content',data).

In most cases I saw concept with a defining model: function() .. in the Route and setupController: function(controller, model) with controller.set('content',model). I like this "set" because 'content' is RecordArray (not PromiseArray) and I can easily use that for datatables and another JavaScript plugins. I think my solution isn't good.

1

1 Answers

0
votes

I think your concept is correct, I have been using the following flow:

In your router:

App.Router.map(function() {
    this.resource('search', { path: '/query/:filters' });
});
App.SearchRoute = Ember.Route.extend({
    model: function(params) {
       return this.store.findQuery('dataModel', {"q": JSON.stringify({"filters": params.filters})});
});

In your html, just bind the action which will lead to the new Search Route, something like below :

<button {{action "doSearch"}}>Search</button>

In your controller:

App.SearchController = Ember.ArrayController.extend({
...
actions: {
    doSearch: function() {
        var query = buildYourQueryObject();
        this.transitionToRoute("search",  query);
    }
}

Upon clicking on the button, the app will transition into your search route, and "query" will be serialized and sent into the Route, and the Route.model() will attempt to be populated based on the serialized parameters provided.

Note: The code has been simplified, you might need to add more stuff in order to make it work