问题
I have instantiated the model using createRecord. Now how do I get the id of that record before it is saved ?
回答1:
You can consider the generateIdForRecord
http://emberjs.com/api/data/classes/DS.Adapter.html#method_generateIdForRecord
If the globally unique IDs for your records should be generated on the client, implement the generateIdForRecord() method. This method will be invoked each time you create a new record, and the value returned from it will be assigned to the record's primaryKey.
Most traditional REST-like HTTP APIs will not use this method. Instead, the ID of the record will be set by the server, and your adapter will update the store with the new ID when it calls didCreateRecord(). Only implement this method if you intend to generate record IDs on the client-side.
You can even create model specific adapter say post
model, you can create post
adapter and override generateIdForRecord
method.
adapters/post.js
import ApplicationAdapter from './application';
import DS from 'ember-data';
import { v4 } from 'uuid';
export default ApplicationAdapter.extend({
generateIdForRecord: function(store, inputProperties) {
return v4();
}
});
来源:https://stackoverflow.com/questions/40628814/how-do-i-get-the-unique-id-of-ember-object-when-object-is-initialized