oneiota
oneiota

Reputation: 341

Ember js wait for controller this.get request / rest adapter lag

I am trying to access my model's content in its respective controller but it initially returns a promise with undefined attributes, then when accessed a second or third time it returns the value.

Could it be there is a delay in retrieving data from the REST adapter?

Also trying to use '.then()' on the request in a controller does not work as I believe it for use only in the route.

App.EquipmentsController = Ember.ArrayController.extend({
    getContentForMapping: function() {
        console.log(this.get('model').objectAt(1).get('contractor.name'));
        //returns undefined first time 
    }
});

What is strange is that when I access the model's properties from the template view in a loop it returns them straight away.

{{#each equipment in model}}
   <p>{{equipment.contractor.name}}</p>
{{/each}}

Below is how I have set up the route. It's grabbing a list of equipment relevant to a specific site:

App.EquipmentsRoute = Ember.Route.extend({
  model: function(){
      //Get the model for the selected site and grab its equipment list.  
      return this.modelFor('site').get('equipment'); 
  },
  //Set the model we just grabbed above as the model to use in the controller
  setupController: function(controller, model) { 
    controller.set('model', model);
  }
});

Upvotes: 3

Views: 693

Answers (2)

oneiota
oneiota

Reputation: 341

Changing our API endpoint to support sideloading seems to have fixed the delay we were experiencing when making the this.get('model') request.

There is a small section in the ember guides that covers this: http://emberjs.com/guides/models/the-rest-adapter/

Upvotes: 2

splattne
splattne

Reputation: 104040

I think the mistake is in your App.EquipmentsRoute's setupController function. Try to set the content property, not the model hook property.

controller.set('content', model);

in

App.EquipmentsRoute = Ember.Route.extend({
  [...] 
  setupController: function(controller, model) { 
    controller.set('content', model);
  }
});

Upvotes: 0

Related Questions