简体   繁体   English

Ember数据从模型中获取“ belongsTo” attr(id),而没有解析实际对象

[英]Ember-data get “belongsTo” attr (id) from model without resolving the actual object

I want to get a models attribute (id) "belongsTo" without forcing a server call or resolving the object. 我想获取模型属性(id)“ belongsTo”,而无需强制服务器调用或解决该对象。 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. 我最近更新为ember-data 1.13.4,而我的旧版本不再起作用。

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. 但是随着新的Ember-Data更新,_data不再包含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 谢谢

Found it. 找到了。 Based on this answer: 基于此答案:

ember-data: How to tell if a model's async: true relationship is loaded without triggering a load? ember-data:如何判断模型的async:真实关系是否已加载而不触发加载?

I was able to figure it out for ember-data 1.13.4 Solution: 我能够找出余烬数据1.13.4解决方案:

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: 编辑:这可以用于扩展具有功能的DS.Model:

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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM