Handling Metadata on an Ember.RSVP.hash


#1

TL;DRLHere’s the stack overflow question

I have a master detail view in my app that shows a list of widgets on the left, and when you select one loads the specific widget and various other information about the widget from a few different API endpoints.

This information is displayed in a few tables, which I’m attempting to paginate I’ve captured the pagination controls in a component, and I’ve set up the queryParams so that when the user changes the pageSize or currentPage properties, the model is refreshed in the route.

So all is going pretty well, but I’m running into an issue with getting the metadata and loading it at the correct time.

So here is the current route:

App.WidgetsShowRoute = Ember.Route.extend
  queryParams:
    infoPageSize:
      refreshModel: true
    infoPage:
      refreshModel: true

  model: (params) ->
    @modelFor('widgets')
    store = @get('store')
    store.find("widget", params.widget_id).then (result) ->
      promises = {
        widget: result
        some_info: ->
          widget = @widget
          store.findQuery("some_info", {
            group_id: widget.get('widgetId')
            per_page: (params.someInfoPageSize || 10)
          })
        some_other_info: ->
          widget = @widget
          store.findQuery("some_other_info", {
            name: widget.get('name')
          })
      }
      Ember.RSVP.hash(promises)
  setupController: (controller, models) ->
    controller.setProperties
      content: models.widget
      someInfo: models.some_info()
      someOtherInfo: models.some_other_info()

Now, the model hook gets fired off again when I interact with my pagination component like I want, however I’m somehwat at a loss on when to set the metadata returned from the API in the controller so that the view represents the current state of the data. I tried just doing it at the controller level, but it appears to be trying to do it before the promise from Ember.RSVP is resolved, so it’s just blank, and when I put it in setupController it seemed to work but the hook is not rerun when the model is refreshed.

So what’s the proper way to handle this scenario? There wasn’t much information in the guide on how metadata fits into the controller/route lifecycle, only how to get it out of the store.


#2

I don’t see how you’re using metadata in the true sense of the word. Ember-data supports metadata objects hanging off response, but this is not that. What you’re trying to do is chain store.finds together and you want setupController to only be hit once they all resolve - correct?

model: function(params) {
    var store = this.get('store');
    
    this.modelFor('widgets'); // <- not sure what that's doing since you're not doing anything with the model that modelFor returns
    
    return store.find("widget", params.widget_id).then(function(result) {
      return Ember.RSVP.hash({
        widget: result,
        some_info: store.findQuery("some_info", {
            group_id: result.get('widgetId'),
            per_page: params.someInfoPageSize || 10
        }),
        some_other_info: store.findQuery("some_other_info", {
            name: result.get('name')
        })  
      });
    });
  }

What you had in your coffeescript is not identical to the code above, in particular you were hanging functions off your promise hash not actual Promises which would cause the RSVP.hash to immediately resolve.