Can't believe something so simple is giving me so much greif! Anyway, I have two models:
App.Basket = DS.Model.extend({
fruits: DS.hasMany('App.Fruit')
});
App.Fruit = DS.Model.extend({
basket: DS.belongsTo('App.Basket')
});
I fetch a basket with id: 1 and don't specify any fruit_ids. Then later on I fetch a fruit with a basket_id of 1. fruit.get('basket')
returns the basket just fine. However, basket.get('fruits')
is empty, it never updates.
Specifying fruit_ids in the basket JSON is not an acceptable solution as in the actual real application a basket may have thousands to millions of fruit and the user won't be needing to fetch them all.
I've tried adding the following on App.Fruit
:
didLoad: function() {
this.get('basket').get('fruits').pushObject(this);
}
Which works fine for the first load. However, if ember-data attempts to fetch the data again then you just get:
Uncaught Error: Attempted to handle event `loadedData` on <App.Fruit:ember353:3> while in state rootState.loaded.updated.uncommitted. Called with undefined
Changing didLoad
to:
didLoad: function() {
this.get('basket').get('fruits').pushObject(this);
this.get('stateManager').send('becameClean');
this.get('stateManager').send('finishedMaterializing');
}
Removes all errors. However, didLoad is never called again even when ember attempts to refetch the data (eg after visiting another route and back again).
Spent a good few hours on this and I can't understand how something so simple has taken up so much of my time. All I want to do it have the hasMany on the basket update when new Fruits are fetched!
I've had the issue with rootState.loaded.updated.uncommitted. The issue here is that ember-data doesn't have a handler for the loadedData event whilst in that state. Unfortunately, there's no simple way to add an event, but I managed to do it by copying the closure that the original definition of the states was in. You can find more details here
http://discuss.emberjs.com/t/extending-ember-data-states/838
And the working code is gisted here:
https://gist.github.com/pzuraq/5304796
I'm having a similar issue with hasMany relationships right now so I'm going to attempt to implement this and see if I can't get it working. I'll let you know if there is any progress on my end.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.