0

I’m building a map with a search function. Basically, I’d like to store objects from the server within my ember app so that whenever I search for something that collection updates itself with the results from the server so the related view updates itself. It’s all on one page.

So far I have an Application Controller, and a Results ArrayController. Data is shown from the Results Controller. Now I’d need that when a search is requested, it gets JSON from the server and updates the results collection.

First question would be: How would you build that?

I did a v1 with jQuery only and started a new one with Ember but I’m lost as of how structure-wise should I build it.

I built a small jsbin based on what I have here: http://emberjs.jsbin.com/IYuSIXE/1/

Second question: How would I change a route's model content? Am I going in the wrong direction?

Thanks a lot

Tommy B.
  • 3,591
  • 14
  • 61
  • 105

2 Answers2

1

You can do both 1 and 2 with query params, check the documentation here https://github.com/alexspeller/website/blob/a96d9afe4506454b155cc64299e86e558ce3c9f1/source/guides/routing/query-params.md

When your route calls the model it will pass the query params, you can do your search against them

  model:function( params, queryParams, transition ) { callToYourBackedEndWithQueryParams}

Second question: How would I change a route's model content? Am I going in the wrong direction?

When the search is requested, in an action you can call this.transitionTo({queryParams: {sort: 'asc'}});, that will fire up again the model hook and you can do the query against your server again.

Adolfo Builes
  • 501
  • 1
  • 3
  • 6
0

What I was looking for is a way to change the model on-the-fly.

So basically if I have this:

App.ResultsRoute = Ember.Route.extend({
    model: function() {
        // empty array that will contain results
        return [];
    }
});

I can do this to set the content of the model:

this.get('model').setObjects([{}, {}, {}]);

That way I can dynamically play with the models, load them with objects coming from almost anywhere.

Tommy B.
  • 3,591
  • 14
  • 61
  • 105