Javascript Ember-通过store或新的ajax调用手动链接到另一个模型

Javascript Ember-通过store或新的ajax调用手动链接到另一个模型,javascript,ember.js,ember-data,ember-cli,Javascript,Ember.js,Ember Data,Ember Cli,恩伯纽伯在这里 // working with Ember : 1.13.6 Ember Data : 1.13.7 __ __ 单个主机包含对位置的引用,但我没有位置id,在查看主机信息时只有“location_uri”,我希望我可以使用它手动查找此记录,并在显示主机显示页面时将其包含在模型中 ……我想显示位置的名称,而不是此uri,我想从主机显示页面将其链接回实际位置。我想知道怎么做 我想我知道如何在我的商店里找到一张这样的唱片: this.store.find('locati

恩伯纽伯在这里

// working with
Ember      : 1.13.6
Ember Data : 1.13.7
__

__

单个主机包含对位置的引用,但我没有位置id,在查看主机信息时只有“location_uri”,我希望我可以使用它手动查找此记录,并在显示主机显示页面时将其包含在模型中

……我想显示位置的名称,而不是此uri,我想从主机显示页面将其链接回实际位置。我想知道怎么做

我想我知道如何在我的商店里找到一张这样的唱片:

this.store.find('location',{uri:this.model('location')})

但是在哪里和什么时候使用是我遇到麻烦的地方。或者,即使我只是手动进行api调用以获取位置数据,并以某种方式将其包含在我的host show模型中,也将是一个有用的技巧


我很确定我不能使用关系,因为它实际上不存在于JSON中,对吗?

实际上,您可以使用关系来实现这一点,但您必须覆盖适配器和/或序列化程序

如果扩展JSONAPISerializer,您可能会将主机响应序列化为以下内容:

{
  id: "1",
  type: "host",
  attributes: {
    active: true
  },
  relationships: {
    location: {
      links: {
        related: "/location/1"
      }
    }
  }
}
假设这是您的
/models/host.js
: 导出DS.Model.extend({ 活动:DS.attr('boolean'), 位置:DS.belongsTo('location') });

这是您的
models/location.js
: 导出DS.Model.extend({ 名称:DS.attr('string') })

然后你可以做类似的事情

this.store.find('host', '1').then(host => console.log(Ember.get(host, 'location.name')))


我建议不要在
.find()
调用中重写
uri
,而是在适配器上设置它,并构建一个好的序列化程序。

实际上,您可以通过关系来实现这一点,但必须重写适配器和/或序列化程序

如果扩展JSONAPISerializer,您可能会将主机响应序列化为以下内容:

{
  id: "1",
  type: "host",
  attributes: {
    active: true
  },
  relationships: {
    location: {
      links: {
        related: "/location/1"
      }
    }
  }
}
假设这是您的
/models/host.js
: 导出DS.Model.extend({ 活动:DS.attr('boolean'), 位置:DS.belongsTo('location') });

这是您的
models/location.js
: 导出DS.Model.extend({ 名称:DS.attr('string') })

然后你可以做类似的事情

this.store.find('host', '1').then(host => console.log(Ember.get(host, 'location.name')))


我建议不要在
.find()
调用中重写
uri
,而是在适配器上设置它,并构建一个很好的序列化程序。

因此,我无法使JSON API关系正常工作或正确制造,但我确实提出了一个简单的解决方案来解决我试图实现的问题

serializers/applicationon.js

import DS from 'ember-data';
import Ember from 'ember';

export default DS.JSONSerializer.extend({

 /** 
  * temporarily create location property
  * parse the id out of the location_uri "/location/1", need just "1"
  */
 normalize: function (type, hash, property) {
    if (!!hash.location_uri) {
      json['location'] = parseFloat(hash.location_uri.match(/\d+/g));
    }
 },

 /** 
  * when submitting C_UD request remove the manufactured location
  */
 serialize: function (snapshot) {
    delete json['location'];
 }

});
import DS from 'ember-data';

var attr = DS.attr;

export default DS.Model.extend({
  uri: attr(),
  active: attr('boolean'),
  location_uri: attr(),
  location: DS.belongsTo('location', {async: true})
});
在我的
主机模型中
我将store belongsTo helper分配给制造位置

models/host.js

import DS from 'ember-data';
import Ember from 'ember';

export default DS.JSONSerializer.extend({

 /** 
  * temporarily create location property
  * parse the id out of the location_uri "/location/1", need just "1"
  */
 normalize: function (type, hash, property) {
    if (!!hash.location_uri) {
      json['location'] = parseFloat(hash.location_uri.match(/\d+/g));
    }
 },

 /** 
  * when submitting C_UD request remove the manufactured location
  */
 serialize: function (snapshot) {
    delete json['location'];
 }

});
import DS from 'ember-data';

var attr = DS.attr;

export default DS.Model.extend({
  uri: attr(),
  active: attr('boolean'),
  location_uri: attr(),
  location: DS.belongsTo('location', {async: true})
});
在我的模板中,我现在可以执行以下操作:

{{#link-to 'location.show' host.location.id }}{{host.location.name}}{{/link-to}}

因此,我无法使JSON API关系正常工作或正确制造,但我确实提出了一个简单的解决方案,以实现我的目标

serializers/applicationon.js

import DS from 'ember-data';
import Ember from 'ember';

export default DS.JSONSerializer.extend({

 /** 
  * temporarily create location property
  * parse the id out of the location_uri "/location/1", need just "1"
  */
 normalize: function (type, hash, property) {
    if (!!hash.location_uri) {
      json['location'] = parseFloat(hash.location_uri.match(/\d+/g));
    }
 },

 /** 
  * when submitting C_UD request remove the manufactured location
  */
 serialize: function (snapshot) {
    delete json['location'];
 }

});
import DS from 'ember-data';

var attr = DS.attr;

export default DS.Model.extend({
  uri: attr(),
  active: attr('boolean'),
  location_uri: attr(),
  location: DS.belongsTo('location', {async: true})
});
在我的
主机模型中
我将store belongsTo helper分配给制造位置

models/host.js

import DS from 'ember-data';
import Ember from 'ember';

export default DS.JSONSerializer.extend({

 /** 
  * temporarily create location property
  * parse the id out of the location_uri "/location/1", need just "1"
  */
 normalize: function (type, hash, property) {
    if (!!hash.location_uri) {
      json['location'] = parseFloat(hash.location_uri.match(/\d+/g));
    }
 },

 /** 
  * when submitting C_UD request remove the manufactured location
  */
 serialize: function (snapshot) {
    delete json['location'];
 }

});
import DS from 'ember-data';

var attr = DS.attr;

export default DS.Model.extend({
  uri: attr(),
  active: attr('boolean'),
  location_uri: attr(),
  location: DS.belongsTo('location', {async: true})
});
在我的模板中,我现在可以执行以下操作:

{{#link-to 'location.show' host.location.id }}{{host.location.name}}{{/link-to}}

仅供参考,我将很快尝试一下。我是否必须使用JSONAPISerializer才能建立关系。我目前只使用JSONSerializer。您可以使用
JSONSerializer
。它只是期待另一种结构。Ember数据在内部使用JSONAPI结构,因此我认为如果必须进行手动转换,应该使用
JSONAPISerializer
JSONSerializer
需要一个带有关系链接的
links
对象。仅供参考,我将很快尝试一下。我是否必须使用JSONAPISerializer才能获得关系。我目前只使用JSONSerializer。您可以使用
JSONSerializer
。它只是期待另一种结构。Ember数据在内部使用JSONAPI结构,因此我认为如果必须进行手动转换,应该使用
JSONAPISerializer
JSONSerializer
需要一个带有关系链接的
links
对象。