简体   繁体   中英

How to access generateIdForRecord in Ember-Data?

I'm trying to develop an application using the Fixture Adapter with Ember-Data.

When I try and create a new object (based on a model I've defined), it won't work unless I specify the ID.

If I specify the ID and do this:

var person = SD.Person.createRecord({
    id: 234,
    name: "test"
});

var person.save();

I get:

Error: assertion failed: An adapter cannot assign a new id to a record that already has an id. had id: 234 and you tried to update it with 234. This likely happened because your server returned data in response to a find or update that had a different id than the one you sent.

Which makes it sound like somehow I'm updating an existing record (I'm not, there's only 2 fixtures for my Person object with ID's of 1 and 2 respectively).

Is Ember trying to save my object twice somehow?

I thought I may have to try and use generateIdForRecord to set the ID, but I can't reference that function no matter what I try.

newBooking.set('id', this.store.generateIdForRecord(this.store, newBooking));
newBooking.set('id', DS.generateIdForRecord(this.store, newBooking));
newBooking.set('id', this.generateIdForRecord(this.store, newBooking));
newBooking.set('id', generateIdForRecord(this.store, newBooking));

TypeError: this.store.generateIdForRecord is not a function

I'm using the latest releases of Ember and Ember-Data (have tried previous releases too). My model is implemented no differently to the TodoMVC tutorial in the Ember guides and in the tutorial nothing fancy needs to be done to manage ID's with the Fixture adapter so I've really no idea what's going on.

How do I create a new Person object (as per my example, just one 'name' field and persist it using Ember-Data's fixture adapter without the aforementioned errors?

In the end, my first code snippet worked fine.

For some reason, I was expecting to see the new object being persisted in the developer console. My models were being listed in a different view so I didn't realize it was actually working as intended.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM