Emberjs - 无法查询嵌入式模型或关联 [英] Emberjs - unable to query for embedded model or association

查看:116
本文介绍了Emberjs - 无法查询嵌入式模型或关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个小提琴的一小部分: http://jsfiddle.net/v2t67/ 显示在我的问题,但我的问题是我无法查询子模型的json,即注释模型。截至目前,我可以使用下面的查询从chrome开发者控制台访问父对象(Post Model),并从包含父对象的结果中访问父对象(Post Model),在控制台中,我可以点击它,我将看到嵌入的注释如图所示在此截图中: http://imgur.com/3WL4I

A small portion of this fiddle: http://jsfiddle.net/v2t67/ is displayed in my question, but my problem is that i am unable to query for the json of the child model ie the comment model. As of now, i can access the parent object (Post Model) from chrome developer console using the queries below and from the result that contains the parent object, in the console, i can click on it and i will see the embedded comments as shown in this screenshot: http://imgur.com/3WL4I.

那么如何使用objectAt(0).toJSON()直接返回来查询评论,需要点击父对象以查看它?

So how will query for the comment using objectAt(0).toJSON() to return it directly with out needing to click on the parent Object inorder to see it?

非常感谢。

 yt = App.store.findAll(App.Post)
 yt.objectAt(0).toJSON()   //to display the json for post

App.Comment = DS.Model.extend({
    content: DS.attr('string'),
    post: DS.belongsTo('App.Post')
});


App.Post = DS.Model.extend({
    body: DS.attr('string'),
    comments: DS.hasMany('App.Comment', { embedded: true } )
});


推荐答案

 App.Comment = DS.Model.extend({
   content: DS.attr('string'),
   post: DS.belongsTo('App.Post')
 });


 App.Post = DS.Model.extend({
   body: DS.attr('string'),
   comments: DS.hasMany('App.Comment', { embedded: true } )
 });

有关完整代码,请参阅:** http://jsfiddle.net/v2t67/ **

For the full code see: ** http://jsfiddle.net/v2t67/**

从源文件读取关联测试后,我找到了一个办法。关联测试如下:
** https://github.com/emberjs/data/blob/master/packages/ember-data/tests/unit/associations_test.js **

After reading the test for association from the source, i found a way to do it. The test for association is here: ** https://github.com/emberjs/data/blob/master/packages/ember-data/tests/unit/associations_test.js**

仍然坚持上述两个模型作为我们的例子,我们可以通过执行以下操作来查询嵌入式模型(注释模型)的json数据:

Still sticking to the two models above as our example, we can query for the json data of the embedded model (Comment model) by doing:

      **Approach 1**

 query = App.store.find(App.Post,  1)
 query.get('comments').objectAt(0).toJSON()

在进行进一步的检查时,我发现上述不行想要父模型的json(Post模型)。这是我如何去json:

While doing further checks, i discovered that the above will not work if you want the json of the parent model (Post model). This is how i go the json for that:

      **Approach 2**

 query = App.store.find(App.Post)
 query.objectAt(0).toJSON()

您将获得一个 TypeError:Object没有方法'objectAt',如果您尝试使用方法1获取Post Model的json数据,并且您将获得 TypeError:无法调用方法'objectAt'未定义,如果您尝试使用方法2获取嵌入式模型的json数据。

You will get a TypeError: Object has no method 'objectAt', if you try to get the json data for the Post Model using approach 1 and you will get TypeError: Cannot call method 'objectAt' of undefined, if you try to get the json data for the embedded model using approach 2.

如果我找到出来一些新东西。

I will update this, if i find out something new.

           **UPDATE**

您可以通过将 {associations:true} 传递给 toJSON()函数来获取嵌入式模型的json数据, / strong>如下所示:

You can get the approach 2 to return the json data for the embedded model without error by passing {associations: true}, to the toJSON() function as shown below:

 query = App.store.find(App.Post)
 query.objectAt(0).toJSON({associations: true})

这篇关于Emberjs - 无法查询嵌入式模型或关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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