Search code examples
ember.jsember-model

Reloading a route's model with server json data and need ember-related opinion


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


Solution

  • 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.