0
votes

I'm using Ember v1.0.0 with Ember-Data v1.0.0-beta.3 and have run into a problem with saving newly-created models. The data needs to be saved on a server whose API is not under our control, and is not RESTful, so we're using a custom adapter.

We take the user through a series of forms to gather the data needed for the model, which we keep as a vanilla JavaScript object until they press the "save" button. At this point, we use store.createRecord and pass in the data object to create the actual model, then call the model's save method.

This calls through to our adapter's createRecord method, which serializes the data and posts it to the server. At this point we have no ID for the model because that gets generated by the server, and is returned in the response to the server request. At this point, we update the model to have the newly-generated ID.

The problem we are having is that after this process, when we navigate to our list of models, there is a "ghost" model there, with no ID, as well as the one with the proper ID. This ghost model only exists only in memory, and is not returned by the server when we refresh the list.

So, my question is: are we going about this the right way, and if so, why is the ghost model appearing and how do we prevent it from happening?

EDIT: here's some additional information. I've tried using store.push instead of store.createRecord when creating the model, but save then throws an error because the record's in the "empty" state.

I've also tried removing the record after the save (when using createRecord to instantiate it) using unloadRecord but this fails because the record's state is either root.loaded.created.uncommitted or root.loaded.created.inFlight, depending on where I place the call to unloadRecord. Anyway, it seems wrong that I should have to manually unload a leftover object - it shouldn't appear at all, and I don't know why it does!

3
What is the value if isNew property of ghost model? - Dmitry Demidenko
@DmitryDemidenko: isNew is true for the ghost model. Am I right in thinking this means Ember Data doesn't consider it as having been saved? - Andy Pye
Yes, you've created it somewhere (possible during edit operation) and didn't save or remove - Dmitry Demidenko

3 Answers

0
votes

You've created the 'new' record somewhere (possible during edit operation) and didn't save or remove.

0
votes

I have run into the same problem recently and the following works fine for me (it is a snippet from one of my controllers):

var router = this.get('target');            
var article = this.store.createRecord('article', { name: name, size: size, quantity: quantity });
article.save().then(function(){
            article.unloadRecord();
            router.transitionTo('articles');
        });

I am calling the "unloadRecord" method from the promise success-callback, which ensures the record has been saved/updated, and then i transition to the "articles" route. Although this works, i too am not convinced this is the correct way to handle record creation.

0
votes

The solution is simple, but not one I've come across elsewhere. By calling store.updateId(record, { id: newId }) after the server call which returns the newly-assigned ID for the model, the ID of the record gets updated to the new value, and the typemap for the model gets updated so that Ember Data becomes "aware" that it already has a model of that type with an ID of newId. That second part is important, otherwise Ember Data will create a second model (with the same ID) if you then call find for that type.