gpt4 book ai didi

javascript - Ember : Change model's primary key with FixtureAdapter

转载 作者:行者123 更新时间:2023-11-30 17:28:22 24 4
gpt4 key购买 nike

我已经尝试了一段时间来更改模型的主键以使用 ember-data 存储。我有一个名为“Addon”的装置,它没有“id”字段,但它们确实有一个“artifactId”字段。我找到了多个解决此问题的建议,但到目前为止都没有奏效,而且其中很多似乎已经过时了。最近的方法似乎是这样的:

App.Adapter.map('App.Addon', {
primaryKey: 'artifactId'
});

这只会给我“未捕获的类型错误:无法读取未定义的属性‘map’”。我必须先以某种方式初始化 map 吗?我发现的大多数示例都使用了 RESTAdapter - 它只能在那里工作吗?

我的 app.js 整体看起来像这样:

window.App = Ember.Application.create();
App.ApplicationAdapter = DS.FixtureAdapter.extend();
App.Adapter.map('App.Addon', {
primaryKey: 'artifactId'
});

最佳答案

我认为,您无法使用 FixtureAdapter 的当前默认值更改模型的主键。

FixtureAdapter 的目的不是提供类似于您的服务器 API 的 JSON 对象,默认情况下,fixture 应该采用规范化形式,因为无论是在您的应用程序还是 View 中,您都将访问 DS.Model 的实例,其primaryKey 始终是 id

{{#with model}}
model({{id}}): {{name}}
{{/with}}

您的服务器可以提供任何主键属性,因此只有在使用您自己的适配器时,您才必须设置模型序列化程序,以便它可以将主键序列化为模型属性 id

container.register('serializer:user', DS.RESTSerializer.extend({
primaryKey: 'artifactId'
}));

下面是一个可以使用 RESTAdapter 或 FixtureAdapter 的示例:

http://emberjs.jsbin.com/batih/1/edit

关于javascript - Ember : Change model's primary key with FixtureAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23736494/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com