A pattern I’ve found myself using a lot is when I need to load multiple models in a single route. This is usually due to a form that needs collections of data to present a choice to the user (a drop down, checkboxes, etc). I’ve typically implemented it using an Ember.RSVP.hash
as the route’s model, and then setupController
to cherry pick the properties and set them on the controller. See http://emberjs.jsbin.com/zirot/5/edit for an example of this pattern.
I’ve done this enough times now, that I’d like to do it all in one step, rather than needing setup controller. I’ve come up with a solution I think I like, and spiked an implementation tonight. Here’s the same example using my Ember.PromiseModel
solution: http://emberjs.jsbin.com/zirot/4/edit.
How do others handle this case? Would this be useful to anyone else?
1 Like
I usually just load all the additional models in the afterModel hook suing RSVP.hash
or RSVP.all
(and sometimes both, chained).
We currently have a mixin called FetchControl
that lets you declare complex chains of fetch dependencies:
expirableObjects: {
show: [
[ 'ticket', 'ticket.requester' ],
'comments',
'macros'
]
}
will fetch ticket
, comments
, and macros
simultaneously, then ticket.requester
after ticket
is fetched, and resolved only when all are done. We wrote this for Ember 0.9, so it doesn’t really have “model” semantics – it’s behavior, not a “thing.” We’ll be reworking it into something more like a richer RSVP.hash
, at which point we’ll gladly share it. It may be, though, that RSVP.all
and RSVP.hash
are sufficient for our needs. The above would be
RSVP.all(
ticket.fetch().then(function(t) { return t.get('requester').fetch() }),
comments.fetch(),
macros.fetch()
)