I have a complicated object graph I'm building up in an Ember Controller.
export default Container({
username: DS.attr('string'),
items: DS.hasMany('item')
})
export default SomeDetail({
foo: DS.attr('string')
})
export default Item({
detail_type: DS.attr('string'),
detail_id: DS.attr('number'),
container: DS.belongsTo('container')
})
So, to set all this up, I'm basically trying to
- Create the conatainer,
- Then, create the details, of which there may be many
- Then, create the items, of which there will be as many of the details
- Wait for all promises to resolve
- Fire off a custom rest action to "activate" the container once it has all of it's stuff.
The code looks like this (coffee), simplified but I think the gist is there
promises = []
@store.createRecord('container',
username: @get('username')
).save().then(container) ->
@get('itemsInMyController').forEach (item) ->
@get('store').createRecord('detail',
# Set Properties
).save().then (detail) ->
item = @get('store').createRecord('item',
# Set Properties
)
promsies.push item
item.save()
Ember.RSVP.allSettled(promsies).then (responses) ->
# Perform Activate Action
When all the promises resolve, everything is how I want it, however, allSettled is firing way too soon, because it's reached before the details have resolved, so the items haven't been created, so there's nothing in the array. This also happens if I add the details to the array, because it's still reached well before the items have been created.
The only thing I can thing of is to have separate arrays tracking the different promises, and having a nested allSettled as each one resolves, but this is starting to feel pretty hairy, and I'm wondering if there's a better way.
Thanks!
Aucun commentaire:
Enregistrer un commentaire