Learning how to create an adapter


#1

Hi, I’m trying to learn more about creating custom adapters in Ember so I started one where I want to create and update records in sessionStorage. I have the createRecord method working, but in the updateRecord method, the id is always null. Any idea why? For the id of models, I am just using a timestamp. Thanks in advance!

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);
    });
  }
});