问题
it's a common pattern to createRecord in the model hook when user enters the route.
Sometimes those records remain unused, and they pops up in certain conditions, i.e. in peekAll query, although that is unwanted. How do deal with them?
回答1:
If creation is the main responsibility for the route, you could destroy model on deactivate
route event, for example:
//route
model: function() {
return this.store.createRecord('modelName');
},
cleanModel: function() {
var model = this.modelFor( this.routeName );
if (model.get('isNew')) {
model.destroyRecord();
}
}.on('deactivate')
Deactivate event is triggered when the router completely exits this route. It is not executed when the model for the route changes.
来源:https://stackoverflow.com/questions/31151515/how-do-you-deal-with-extra-records-that-was-created-but-not-used