Setting model content on auxiliary controllers

First off, I’m not using em-data for loading my data. I have a model that does an ajax callback and gets back a fairly heavily nested JSON structure. I create some Ember.Objects about of that structure and return them in the then callback of the ajax call. Then in the route, I am loading it in setupController like:

App.ReportsIndexRoute = Ember.Route.extend({
  setupController: function(){
    var summary = App.ReportsAdapter.find();
    var controller = this.controllerFor("punctualitySummary");
    controller.set("content", summary);
  }
});

Unfortunately, when it gets to the template, it doesn’t update once the request is complete. However, if I make it a proper model for the route, the template updates just fine:

App.ReportsIndexRoute = Ember.Route.extend({
  model: function(){
    return App.ReportsAdapter.find()
  },
  setupController: function(_,model){
    var controller = this.controllerFor("punctualitySummary");
    controller.set("content", model);
  }
});

This makes me think my approach is completely wrong, but I need to load multiple models for this route. How can I do that without using em-data?

Thanks!

What does the .find() method return?

Here you go (ajax is just a $.ajax shortcut that provides my defaults).

 ReportsAdapter.reopenClass({
    find: function(){
      var model = new ReportModel();
      var self = this;
      return ajax("/stat-group/punctuality/summary.json").then(function(data){
        var parsed = self.parse(data);
        model.reopen(parsed);
        return model;
      });
    }
 });

This is how I would do it:

ReportsAdapter.reopenClass({
find: function(){
  var model = new ReportModel();
  var self = this;
  ajax("/stat-group/punctuality/summary.json").then(function(data){
    var parsed = self.parse(data);
    // 2. Update the model with data from JSON
    model.setProperties(...);
  });
  // 1. Return an empty model first
  return model;
}

});

That’s how I was doing it originally, but it wasn’t working. I’ll try that again.

Ah, I bet it was because I was calling “reopen” not “setProperties”. It was probably never publishing the property change.

1 Like