使用Ember-Data 1.0Beta设置多态别名 [英] Setting an Polymorphic Alias with Ember-Data 1.0Beta

查看:82
本文介绍了使用Ember-Data 1.0Beta设置多态别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了在Ember.js中使用多态关系,您需要配置适配器以识别多态模型的别名,如这里

In order to use polymorphic relationships in Ember.js, you need to configure your adapter to recognize an alias for the polymorphic model, as documented here:

DS.RESTAdapter.configure('App.Post', {
  alias: 'post'
});

不幸的是,这种方法不再适用于Ember Data 1.0Beta,因为您无法再配置适配器。相反,你必须扩展它们。然而,只需这样做就不行:

Unfortunately, this approach no longer works with Ember Data 1.0Beta, as you can no longer configure adapters. Instead, you have to extend them. Simply doing this doesn't work, however:

DS.ActiveModelAdapter.extend('App.Post', {
  alias: 'post'
});

它会抛出错误:

Expected hash or Mixin instance, got [object String]

Ember-Data转换的href =https://github.com/emberjs/data/blob/master/TRANSITION.md#rest-adapter-and-serializer-configuration =nofollow>部分指南详细介绍了适配器和串行器的新方法。我不知道如何翻译这个建议,例如别名:'post'。该指南介绍了有关如何处理有效负载的大量细节,但我不知道别名是否适合该处理。

This section of Ember-Data's transition guide goes into detail about the new approach to adapters and serializers. I'm not sure how to translate that advice for something like alias: 'post', however. The guide goes into a great deal of detail about how payloads are processed, but I don't know where the alias was suppose to fit into that processing.

推荐答案

在顶部已经过时了,请参阅转换文档中的多态性部分 https://github.com/emberjs/data/blob/master/TRANSITION.md#polymorphic-relationships

That's out of date at the top, see the polymorphism portion in the transition documentation https://github.com/emberjs/data/blob/master/TRANSITION.md#polymorphic-relationships

多态关系

多形态类型现在使用型号名称+Type的json键进行序列化

Polymorphic types are now serialized with a json key of the model name + "Type"

例如给定多态关系:

 App.Comment = DS.Model.extend({
   message: DS.belongsTo('message', {
     polymorphic: true
   })
 });

Ember Data 0.13

Ember Data 0.13

 {
   "message": 12,
   "message_type": "post"
 }

Ember Data 1.0.beta.3:

Ember Data 1.0.beta.3:

 {
   "message": 12,
   "messageType": "post"
 }

另一个注意事项,如果你再次看到这个错误,那就是抱怨这个

On another note if you ever see that error again, it's complaining about this

 DS.ActiveModelAdapter.extend('App.Post', {
   alias: 'post'
 });

ember对象的任何扩展名的第一个参数都需要一个哈希或混合,你正在发送一个字符串Aka它想要一个实际的类(需要在你达到这段代码之前定义)。

the first parameter of any extension of an ember object expects a hash or mixin, and you are sending it a string. Aka it wants an actual Class (it needs to have been defined before you get to this portion of your code).

 DS.ActiveModelAdapter.extend(App.Post, {
   alias: 'post'
 });

这篇关于使用Ember-Data 1.0Beta设置多态别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆