Setting model content on auxiliary controllers


#1

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!


#2

What does the .find() method return?


#3

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;
      });
    }
 });

#4

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;
}

});


#5

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


#6

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