我正在尝试了解有关在Ember中创建自定义适配器的更多信息,因此我启动了一个我想在sessionStorage中创建和更新记录的地方。我有createRecord方法,但在updateRecord方法中,id始终为null。知道为什么吗?对于模型的ID,我只是使用时间戳。
export default DS.Adapter.extend({
createRecord(store, type, snapshot) {
var data = this.serialize(snapshot, { includeId: true });
return new Ember.RSVP.Promise((resolve, reject) => {
var comments = {}, id = Date.now();
if (window.sessionStorage.getItem('comment')) {
comments = JSON.parse(window.sessionStorage.getItem('comment'));
}
data.id = id;
comments[id] = data;
window.sessionStorage.setItem('comment', JSON.stringify(comments));
resolve(data);
});
},
updateRecord(store, type, snapshot) {
var data = this.serialize(snapshot, { includeId: true });
var id = snapshot.id;
console.log('updating', id); // id is null. Why???
return new Ember.RSVP.Promise((resolve, reject) => {
resolve(data);
});
}
});
我知道我需要创建一些其他方法,比如findAll(),findRecord()等,但不会有这些导致这个问题?我不确定我错过了什么。提前谢谢!