Confused about Ember Data 1.13 Serialization for side-loaded data

I moved away from ember a bit and I recently reverted to 1.13 with Ember-Data 1.13. Initially, I was in awe of the new JSON API formatting, as I thought it would easily support side-loaded posts as per the example provided in the change docs.

{
  data: { 
    id: '1', 
    type: 'user', 
    attributes: {
      name: 'wecc'
    }, 
    relationships: {
      accounts: {
        data: [
          { id: '1', type: 'account' },
          { id: '2', type: 'account' }
        ]
      }
    }
  },
  included: [{ 
    id: '1',
    type: 'account',
    attributes: {
      email: 'wecc@sweden.se'
    }
  }, {
    id: '2',
    type: 'account',
    attributes: {
      email: 'wecc@greece.gr'
    }
  }]
}

      

I realized that if I normalized the payload above, then it would handle my relationship for me and create store entries. This does not seem to be the case, and I was hoping someone could explain what I should do to create the store entries correctly.

To be clear, this is the code I am running from my controller,

getData: function() {
  var self = this;
  this.store.findRecord("user", 1).then(function(user) {
    console.log(user);
    console.log(user.get('name'));
    console.log(self.store.peekAll('account'));
  });

}

      

And I expect peekAll to show me two entries, but it doesn't. The user and user.get ('name') are registered. Thank!

UPDATE: Now I get a console error Cannot read property '_internalModel' of undefined

whenever I try to download it. This is in the function

function _find(adapter, store, typeClass, id, internalModel, options) {
  var snapshot = internalModel.createSnapshot(options);
  var promise;
  if (!adapter.findRecord) {
    Ember.deprecate("Adapter#find has been deprecated and renamed to `findRecord`.");
    promise = adapter.find(store, typeClass, id, snapshot);
  } else {
    promise = adapter.findRecord(store, typeClass, id, snapshot);
  }
  var serializer = serializerForAdapter(store, adapter, internalModel.type.modelName);
  var label = "DS: Handle Adapter#find of " + typeClass + " with id: " + id;

  promise = Promise.cast(promise, label);
  promise = _guard(promise, _bind(_objectIsAlive, store));

  return promise.then(function (adapterPayload) {
    Ember.assert("You made a request for a " + typeClass.typeClassKey + " with id " + id + ", but the adapter response did not have any data", adapterPayload);
    return store._adapterRun(function () {
      var requestType = get(serializer, "isNewSerializerAPI") ? "findRecord" : "find";
      var payload = normalizeResponseHelper(serializer, store, typeClass, adapterPayload, id, requestType);
      //TODO Optimize
      var record = pushPayload(store, payload);
      return record._internalModel;
    });
  }, function (error) {
    internalModel.notFound();
    if (internalModel.isEmpty()) {
      internalModel.unloadRecord();
    }

    throw error;
  }, "DS: Extract payload of '" + typeClass + "'");
}

      

Also for reference are the models I'm using (I've tried with and without inline always)

var User = DS.Model.extend({
  name: DS.attr('string'),
  accounts: DS.hasMany('account', {embedded: 'always'})
});
var Account = DS.Model.extend({
  email: DS.attr('string'),
  user: DS.belongsTo('user')
});

      

+3


source to share





All Articles