由hasMany关系定义的Ember孩子突然改变为嵌入式模型 [英] Ember children defined by a hasMany relationship suddenly change to embedded model

查看:119
本文介绍了由hasMany关系定义的Ember孩子突然改变为嵌入式模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下模型定义:

App.Post = DS.Model.extend({
  title: DS.attr('string'),
  comments: DS.hasMany('comment')
});

App.Comment = DS.Model.extend({
  message: DS.attr('string')
});

如果我创建一个带有注释的Post条目,存储在我的浏览器中的JSON本地存储引用注释作为一个非常有效的ID数组:

If I create a Post entry with a Comment, the JSON stored in my browsers local storage references the Comments as an array of IDs which works great:

...
"o3duh":{
    "id":"o3duh",
    "title":"How to write Ember",
    "comments":[
        "jf0a2"
    ]
}
...

但是,当我添加另一个帖子的那一刻,JSON突然改变了这样的评论嵌入:

However, the moment I add another Post, the JSON suddenly changes such that Comments are embedded:

...
"o3duh":{
    "id":"o3duh",
    "title":"How to write Ember",
    "comments":[
        {
            "message":"First!"
        }
    ]
},
"6kudl":{
    "id":"6kudl",
    "title":"Learning Ember is painful",
    "comments":[
    ]
}
...

为什么会发生这种情况?我可以防止它吗这导致我的问题,因为一旦它改变成这种嵌入的格式,LSAdapter在重新加载页面时不能读取数据。

Why is this happening? Can I prevent it? This is causing me problems because once it changes into this embedded format, the data cannot be read by the LSAdapter when reloading the page.

这是一个 JSBin ,所以你可以看到它发生在你身上,看到完整的JSON等。要重现问题,只需创建一个帖子添加评论,然后您可以刷新页面没有问题。然后添加另一个帖子并尝试刷新页面。

Here is a JSBin so you can see it happen for yourself and see the full JSON etc. To reproduce the problem, just create a post and add a comment then you can refresh the page without problem. Then add another post and try to refresh the page.

我不知道问题是与ember数据还是本地存储适配器。

I'm not sure if the problem is with ember-data or the localstorage adapter.

推荐答案

我通过修改LocalStorageAdapter来解决问题,这样它只会尝试以预期的格式保留JSON。

I solved the problem by modifying the LocalStorageAdapter so that it only attempts to persist JSON in the expected format.

您可以在这里查看我提交给原作者的拉动请求: https://github.com/rpflorence/ember-localstorage-adapter/pull/26

You can see the pull request I submitted to the original author here: https://github.com/rpflorence/ember-localstorage-adapter/pull/26

希望将其折叠到LSAdapter项目中,或更好的是,有人会想出一个更好的解决方案;)

Hopefully it will either get folded into the LSAdapter project, or better still, someone will come up with a better solution ;)

这篇关于由hasMany关系定义的Ember孩子突然改变为嵌入式模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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