Emberjs数据如何加载hasMany数据后 [英] Emberjs Data How to load hasMany-Data later

查看:107
本文介绍了Emberjs数据如何加载hasMany数据后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下Emberjs数据模型:

  App.File = DS.Model。 extend({
like:DS.attr('boolean'),
comments:DS.hasMany('App.Comment')
});

App.Comment = DS.Model.extend({
file:DS.belongsTo('App.File'),
comment:DS.attr('string')
});

并预加载:

  App.store.load(App.File,{id:1,like:false}); 

现在我想,如果我得到这样的评论:

  var f = App.store.find(App.File,1); 
var c = f.get(comments);

var c是一个空的EmberArray,请求被发送到服务器。但我没有得到请求?为什么我该怎么做?我真的不想预先加载评论。



此外,如果我添加评论,还要同时更改文件:

  f.get(comments)。createRecord({comment:test}); 
f.set(like,true);
App.store.commit();

两个请求发送到服务器。但是如果我返回以下JSON(对于该文件):

  {id 1,like:true} 

我的第一个可见的评论再次消失。为什么?谢谢你的帮助!

解决方案

关于您问题的第一部分:您应该在文件数据中填写评论ID:

  App.store.load(App.File,{id:1,like:false,comments:[1,2,3]}); 

所以,在需要的时候,这些评论会被加载。


$ b $关于第二部分:您当然不会将服务器的回复中的注释ID序列化,因此,您将在描述一个空的注释列表时重置注释。



你应该至少返回如前所示的注释ids ...这两个问题是链接的。


I have the following Emberjs Data model:

App.File = DS.Model.extend({
  like: DS.attr('boolean'),
  comments: DS.hasMany('App.Comment')
});

App.Comment = DS.Model.extend({
  file: DS.belongsTo('App.File'),
  comment: DS.attr('string')
});

And preload it with:

 App.store.load(App.File, {id: 1, like: false});

Now I thought, if I get the comments like this:

var f = App.store.find(App.File, 1);
var c = f.get("comments");

var c is a empty EmberArray and a request is send to the server. But I don't get a request? Why and how do I have to do it? I really don't wanna preload the comments.

Further more, if I add a comment, but also change the file simultaneously:

f.get("comments").createRecord({comment: "test"});
f.set("like", true);
App.store.commit();

Two requests are send to the server. But if I then return the following JSON (for the file):

{ "id": 1, like: true }

My first visible comment disappears again. Why? And what do I have to do?

Thanks for your help!

解决方案

Regarding the first part of your question: You should populate the comments ids in the file's data:

App.store.load(App.File, {id: 1, like: false, comments: [1, 2, 3]});

So the comments will be lazy loaded when needed.

Regarding the second part: You certainly do not serialize the comment ids on the reply from your server, so the comments are reset, as you describe an empty comments list.

You should at least return the comments ids as shown just before... The two issues are linked.

这篇关于Emberjs数据如何加载hasMany数据后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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