简体   繁体   English

parse.com +主干为用户检索嵌套的对象集

[英]parse.com + backbone retrieve nested set of objects for a user

I'd like to extend this example that parse.com gives in its tutorials to work with backbone collections and a second level of data (comments). 我想扩展parse.com在其教程中提供的该示例 ,以使用主干集合和第二层数据(注释)。 Right now the exmaple is retrieving a list of posts for a user, with the following data structure: 现在,示例正在使用以下数据结构检索用户的帖子列表:

USER
--------------------------------------
| User_ID |  Username | ......
--------------------------------------
| 1       |  John     | ........
| 2       |  Jane     | ........


POST 
--------------------------------------
| POST_ID  |  User_ID | Title    | .....
--------------------------------------
| 20       |  1       | abcdefg  | .....
| 21       |  1       | svsvdsv  | .....

I'd to extend this call to also return corresponding comments to each post. 我想扩展此调用,以便还向每个帖子返回相应的评论。 Meaning there would be one api call to parse that returns all the posts and all the comments to those posts (properly nested) for the logged in user. 这意味着将有一个 api调用来解析,以返回登录用户的所有帖子和对这些帖子(正确嵌套)的所有注释。 Below is an example of a comment data structure: 以下是注释数据结构的示例:

COMMENT
-----------------------------------------------------------------------
| COMMENT_ID  |  POST_ID  |  Comment_Text        | Title    | .....
-----------------------------------------------------------------------
| 30          |  21       |  awesome post woohoo | abcdefg  | .....
| 31          |  21       |  terrible post....   | svsvdsv  | .....

Any help would be greatly appreciated! 任何帮助将不胜感激!

Backbone.js not support nested models so you can use Backbone-relational.js for nested models, Backbone.js不支持嵌套模型,因此您可以将Backbone-relational.js用于嵌套模型,

Backbone-relational.js provides one-to-one, one-to-many and many-to-one relations between models for Backbone Backbone Relation Backbone-relational.js为Backbone Backbone Relation提供模型之间的一对一,一对多和多对一关系

classes

User = Backbone.Relational.Model({
    defaults : {
        id : '',
        name : '',
        posts : [], //collection
    },
    relation : [{
        type : 'HasMany',
        key : 'posts',
        relatedModel : 'com.dw.attendance.model.Post',
        reverseRelation : {
            key : 'user'
        }
    }]
});

Post = Backbone.Relational.Model({
    defaults : {
        id : '',
        user : '', //model
        comments : '', //collection
        title : '',
    },
    relation : [{
        type : 'HasOne',
        key : 'user',
        relatedModel : 'com.dw.attendance.model.User',
        reverseRelation : {
            key : 'posts'
        }
    },{
        type : 'HasMany',
        key : 'comments',
        relatedModel : 'com.dw.attendance.model.Comment',
        reverseRelation : {
            key : 'post'
        }
    }]
});


Comment = Backbone.Relational.Model({
    defaults : {
        id : '',
        post : '',//model
        text : '',
        title : ''
    },
    relation : [{
        type : 'HasOne',
        key : 'post',
        relatedModel : 'com.dw.attendance.model.Post',
        reverseRelation : {
            key : 'comments'
        }
    }]
});

your data like this, for user : {id : 1, name : 'john', posts : [1,2,3]}; 您的数据是这样的,用户: {id : 1, name : 'john', posts : [1,2,3]};

then you can get, comments of any user post by, 那么您可以获得任何用户发帖的评论,

user.get('posts').get('post_ID').get('comments');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM