Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ERROR when Delete and persist record #1669

Closed
Lancezh opened this issue Jan 17, 2014 · 4 comments
Closed

ERROR when Delete and persist record #1669

Lancezh opened this issue Jan 17, 2014 · 4 comments

Comments

@Lancezh
Copy link

Lancezh commented Jan 17, 2014

Hi,

When I tried to delete and persist a record, I got this error

Error: Attempted to handle event `willCommit` on  while in state root.deleted.saved. 

this is my code

var _temp_model = this.store.getById('project', '_temp_id');
if(_temp_model){
      _temp_model.deleteRecord();
      _temp_model.save();
     // or
     // _temp_model.destroyRecord();
}

I use the lasted canary version

What is the difference with this coding
http://emberjs.com/guides/models/creating-and-deleting-records/#toc_deleting-records

What should I do?

@bmac
Copy link
Member

bmac commented Jan 17, 2014

Hi @Lancezh. Records in ember have various states they can be in that correspond to the record's relationship the the external data source. These states contain information about if a record has been created but not saved to the external data source, if it has unsaved changes, if it is up to date ect.

Internally Ember Data sends events to the record object to transition between state.

The problem here is Ember Data is sending a willCommit event to the record while it is in the "Deleted and saved to the external data source" state. willCommit is the event ember uses internally to denote that it intends to save the record remotely.

Ember Data does not implement a handler for the willCommit event when the record is in this state. Presumably because it doesn't make much sense to delete a record that the external data source has already been deleted.

@bradleypriest @igorT @stefanpenner Should ember data implement a noop willCommit on the root.deleted.saved state to prevent bugs like this?

I suspect the reason why your _temp_model is in this state is because it was created locally but never saved to the external system. A simple workaround for your code would be to check the isDirty property which will return true if the record has any unsaved changes. Your new code might look something like this:

var _temp_model = this.store.getById('project', '_temp_id');
if(_temp_model){
      _temp_model.deleteRecord();
     if (_temp_model.get('isDirty')) {
        _temp_model.save();
     }
}

@Lancezh
Copy link
Author

Lancezh commented Jan 18, 2014

Hi, @bmac . Thank you very much for your patience to explain.
I think I know where is the problem.

Where can I find some docs that explain the various states and how they change between state?
Thanks again.

@bmac
Copy link
Member

bmac commented Jan 19, 2014

The best docs on this that currently exist would be the api docs for DS.RootState. I've heard some criticism that it isn't in depth enough so let me know if there is something missing that you'd like to see.

@igorT
Copy link
Member

igorT commented May 26, 2014

I merged the PR that fixes this, so you shouldn't be having this issue anymore. Thanks for opening it though!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants