Reputation: 51
I'm trying to put together what I think is a pretty straightforward ember delete action (based on this example: http://discuss.emberjs.com/t/migrating-from-ember-data-0-13-to-1-0-0-beta-1-my-findings/2368) from an Index Controller and I think I must be missing something.
actions: {
deleteZone: function (zone) {
if (confirm("Are you sure you want to delete the zone?")) {
var _this = this;
zone.deleteRecord();
zone.save().then(
function () {
_this.transitionToRoute('zones.index');
},
function (error) {
zone.rollback();
}
);
}
}
}
I'm running into trouble when I try to delete a zone that has a corresponding dependency. In this case, the server (Rails 4) throws an exception and returns the following JSON:
{"status":422,"message":"Cannot delete record because of dependent projects","errors":{}}
However, while I believe the server returns the correct error, the UI seems to fail before it gets that far. If I put a debugger on the line after zone.rollback() inside the catch function I get this error:
Attempted to handle event `becameInvalid` on <App.Zone:ember1276:6> while in state root.deleted.inFlight. Called with {}.
I'm running on ember 1.4.0-beta.1, ember-data 1.0.0-beta.4 (ActiveModelAdapter) and rails 4.0.1. Any suggestions would be much appreciated, thanks!
Upvotes: 3
Views: 861
Reputation: 1
I ran into to this issue as well. Running model.transitionTo('loaded.saved');
helped to vaoid any errors thrown, but the model is destroyed as well.
If one wants to keep the model in the store, one must re-inject it, which seems odd, but works:
var model = this.get('model');
var store = model.store;
model.deleteRecord();
model.save().catch(function(err){
model.transitionTo('loaded.saved');
var payload = model.serialize({includeId: true});
store.unloadRecord(model)
store.pushPayload('nestedSet',{nested_set:payload});
});
I am running:
DEBUG: -------------------------------
DEBUG: Ember : 1.8.0-beta.1+canary.d6d4f01d
DEBUG: Ember Data : 1.0.0-beta.9
DEBUG: Handlebars : 1.3.0
DEBUG: jQuery : 1.11.1
DEBUG: -------------------------------
For version testing completeness same happens on ember-data#1.0.0-beta.7
!
Hope it helps, if anyone runs into this too.
Upvotes: 0
Reputation: 51
Manually transitioning to a loaded.saved state after the rollback seems to resolve the issue:
zone.transitionTo('loaded.saved');
After upgrading to the latest ember/ember-data and slightly modifying the JSON response I'm now able to extract the error message out of the JSON using the error reference passed in to the catch expression.
{"status":422,"message":"translation missing: en.Invalid zone","errors":{"base":["Cannot delete record because dependent projects exist"]}}
And ember versions:
DEBUG: Ember : 1.4.0-beta.1+canary.4d69bca7 ember.js?body=1:3307
DEBUG: Ember Data : 1.0.0-beta.5+canary.2e773365 ember.js?body=1:3307
DEBUG: Handlebars : 1.0.0 ember.js?body=1:3307
DEBUG: jQuery : 1.10.2
Upvotes: 2