Ember Data,如何使用registerTransform [英] Ember Data, how to use registerTransform

查看:80
本文介绍了Ember Data,如何使用registerTransform的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在谷歌一段时间,但没有找到任何好的解决方案。

I've been Googling for a while now, but haven't found any good solution.

问题的根源在于我的记录不是使用此方法时设置为 isDirty

The root of the problem is that my records aren't being set to isDirty when using this method:

DS.JSONTransforms.object = {
  deserialize: function(serialized) {
    return Ember.isNone(serialized) ? {} : serialized;
  },
  serialize: function(deserialized) {
    return Ember.isNone(deserialized) ? {} : deserialized;
  }
}

从我收集到的是一个旧的方法,显然仍然工作,因为它处理我抛出的JSON对象,但是在进行修改时不会将我的记录设置为 isDirty

From what I gather this is an old method that apparently still works, since it handles the JSON objects I'm throwing at it, but it's not setting my records to isDirty when making edits.

您现在应该使用的是 registerTransform (根据这个 https://github.com/emberjs/data/issues/517 )。但是我的自定义转换没有注册,所以我想我把它放在错误的地方(与之前的JSONTransforms相同的地方)。

What you now should be using is registerTransform on your adapter (according to this https://github.com/emberjs/data/issues/517). But my custom transform isn't being registered, so I guess I'm putting it at the wrong place (same place as my previous JSONTransforms).

DS.RESTAdapter.registerTransform('object', {
  deserialize: function(serialized) {
    return Em.none(serialized) ? {} : serialized;
  },
  serialize: function(deserialized) {
    return Em.none(deserialized) ? {} : deserialized;
  }
});

任何人都有知识可以分享这个?

Any one have knowledge to share about this?

推荐答案

isDirty 的问题不是因为您没有使用 registerTransform ,行为将是一样的。

The issue with isDirty is not because you're not using registerTransform, the behavior will be the same.

现在,Ember Data不支持对象属性,其中一个难点实际上是观察更改来设置 isDirty 标志。

For now, Ember Data does not support object attributes, one of the difficulty is in fact to observe the changes to set the isDirty flag.

有一个公开问题为此可以在github上跟踪。

There is an open issue for this that you can track on github.

解决方法是将嵌套对象声明为正确的 DS.Model 并在它们之间设置一个嵌入式关系。

A workaround would be to declare the nested objects as proper DS.Model and set an embedded relationship between them.

举个例子,假设你有一个日期对象被发布:

For the example, let's say you have a date object sended with a post :

{post: {
  id: 12
  title: "EmberData accept object attributes, you do not need this anymore !!"
  date: {
    day: "01"
    month: "03"
    year: "2013"
  }
}}

您将声明模型如下:

App.Post = DS.Model.extend({
  title: DS.attr('string'),
  date: DS.belongsTo('App.PostDate')
});

App.PostDate = DS.Model.extend({
  post: DS.belongsTo('App.Post'),
  day: DS.attr('string'),
  month: DS.attr('string'),
  year: DS.attr('string'),
});

并在适配器中添加映射:

And add a mapping in your adapter :

DS.RESTAdapter.map('App.Post',{
  date:{
    embedded:'always'
  }
})

请参阅这个答案有关嵌入式映射选项的更多细节。

See this answer for more detail about the embedded mapping option.

这篇关于Ember Data,如何使用registerTransform的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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