0

I want to get a models attribute (id) "belongsTo" without forcing a server call or resolving the object. I got memberships having a user. The most users are loaded and so I only want to make a server call for some of the users. I updated recently to ember-data 1.13.4 and my old version is't working anymore.

Memberships:

user: DS.belongsTo('user', { async: false }),

Until now I did this to check if a users was already in the store:

if (this.store.peekRecord('user', parseInt(membership._internalModel._data.user)) === null)
      return false;

But with the new Ember-Data update the _data does not include anymore the belongsTo - Ids.

Ends up in this on progress:

Error while processing route: workgroups Assertion Failed: You looked up the 'user' relationship on a 'membership' with id 21 but some of the associated records were not loaded.

I really really appreciate every help on this.

Thx

PowPi
  • 164
  • 1
  • 1
  • 10

1 Answers1

1

Found it. Based on this answer:

ember-data: How to tell if a model's async: true relationship is loaded without triggering a load?

I was able to figure it out for ember-data 1.13.4 Solution:

membership._internalModel._relationships.initializedRelationships.user.canonicalState.id

This is ugly. Any better ideas?

EDIT: This can be used to extend the DS.Model with a function:

import DS from 'ember-data';

export default {
  name: 'model-getid',
  initialize: function() {
    DS.Model.reopen({
      getId: function(key) {
        // TODO(sn): support hasMany as well
        const rel = this._internalModel._relationships.initializedRelationships;
        return rel && rel[key] && rel[key].canonicalState && rel[key].canonicalState.id;
      }
    });
  }
};

now it's possible to us it like this on single relation:

membership.getId('organisation')
Community
  • 1
  • 1
PowPi
  • 164
  • 1
  • 1
  • 10