orbitjs / ember-orbit

Ember.js data layer built with Orbit.js
MIT License
320 stars 42 forks source link

Access to relationships.*.data.id #400

Open ef4 opened 2 years ago

ef4 commented 2 years ago

Given a server that includes data references for unloaded relationships:

{
  relationships: {
    organization: {
      data: { type: "organizations", id: "2" },
      links: { related: "https://example.com/org/2" }
    }
  }
}

Is there public API for accessing the data.type and data.id from an ember-orbit model?

I was able to work around by creating a getter on my model:

get organizationId() {
  let { type, id } = this.$getData().relationships.organization.data;
  return this.$store.keyMap.idToKey(type, 'remoteId', id);
}
dgeb commented 2 years ago

If you have a key defined on your Organization model (@key() remoteId;) and relationship data is included with the primary model's data, then I believe that a minimalist record (with just type, id, and remoteId) of type organization should exist in your cache after processing that payload. If that's true, then you should be able to access it through your model via this.organization.remoteId.

I may be missing some context here. Is this.organization not present? Was it explicitly deleted? Do you have any inverse relationships present?

ef4 commented 2 years ago

Oh, interesting! I was just assuming this.organization would be undefined. I will try that.

ef4 commented 2 years ago

I got a chance to try this, and it's unreliable.

It works if the related record happened to already load via other means. But if the record is not loaded, remoteId is not present. Only type and id are present.

https://github.com/orbitjs/ember-orbit/pull/403 has a passing test to demonstrate the loaded case and a failing test for the unloaded case.