映射到JSON Backbone.js的集合 [英] Mapping JSON to backbone.js collections

查看:132
本文介绍了映射到JSON Backbone.js的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,它看起来像我需要一个提示点我在正确的方向。这个问题是两部分 - 与MULT维JSON和集合集合从JSON工作

Alright, it looks like I need a hint to point me in the right direction. This question is two part - working with mult-dimensional JSON and Collections of Collections from JSON.

我有一些JSON的将是从服务器中检索,并有超过它如何被格式化的控制。

I have some JSON that is going to be retrieved from a server and have control over how it could be formatted.

我有一些麻烦,能够模型连接到的JSON的部分。说我想只呈现每个职位的作者姓名的内容状态的在下面的示例JSON。我有没有问题,让状态到模型,但作者的名字我有点困惑如何得到它。从我的理解我不得不重写解析。

I'm having some trouble being able connecting the model to the parts in the JSON. Say I wanted to render just each of the posts author name, and the content of status in the sample JSON below. I'm having no problem getting the status into the model, but the author name I'm a bit confused how to get to it. From my understanding I have to override the parse.

这是不好的标准/有一个更好的JSON结构,我应该使用?它会更好,以保持尽可能平坦?这就是移动的作者姓名和照片上一级?

Is this bad standards / is there a better JSON structure I should use? Would it be better to keep it as flat as possible? That is move the author name and photo up one level?

我读<一个href=\"http://stackoverflow.com/questions/8782619/how-to-build-a-collection-model-from-nested-json-with-backbone-js\">How建立从Backbone.js的嵌套JSON集合/模型,但它仍然是一个小我不清楚。

I was reading How to build a Collection/Model from nested JSON with Backbone.js but it is still a little unclear to me.

有一个很好的方法,使Backbone.js的为一个集合中的一个集合?我将有岗位的集合,然后将有上发表评论的集合。正如我在骨干正在开发的是,即使可能吗?

Is there a nice way to make a collection within a collection for backbone.js? I will have a collection of posts, and then would have a collection of comments on that post. As I'm developing in backbone is that even possible?

从我在集合和<一个 Backbone.js的集合理解href=\"http://stackoverflow.com/questions/13675539/backbone-js-collection-of-collections-issue\">Backbone.js的数据库发表集合,它会是这个样子?

From what I understand in Backbone.js Collection of Collections and Backbone.js Collection of Collections Issue, it would look something like this?

var Comments = Backbone.Model.extend({
    defaults : {
      _id : "",
      text : "",
      author : ""
    }
})

var CommentsCollection = Backbone.Collection.extend({ model : Comments })

var Posts = Backbone.Model.extend({
    defaults : {
        _id : "",
        author : "",
        status : "",
        comments : new CommentsCollection
    }
})

var PostsCollection = Backbone.Collection.extend({ model : Posts })

样品JSON

{
"posts" : [
    {
        "_id": "50f5f5d4014e045f000002",
        "author": {
            "name" : "Chris Crawford",
            "photo" : "http://example.com/photo.jpg"
        },
        "status": "This is a sample message.",
        "comments": [
                {
                    "_id": "5160eacbe4b020ec56a46844",
                    "text": "This is the content of the comment.",
                    "author": "Bob Hope"
                },
                {
                    "_id": "5160eacbe4b020ec56a46845",
                    "text": "This is the content of the comment.",
                    "author": "Bob Hope"
                },
                {
                ...
                }
        ]
    },
    {
        "_id": "50f5f5d4014e045f000003",
        "author": {
            "name" : "Chris Crawford",
            "photo" : "http://example.com/photo.jpg"
        },
        "status": "This is another sample message.",
        "comments": [
                {
                    "_id": "5160eacbe4b020ec56a46846",
                    "text": "This is the content of the comment.",
                    "author": "Bob Hope"
                },
                {
                    "_id": "5160eacbe4b020ec56a46847",
                    "text": "This is the content of the comment.",
                    "author": "Bob Hope"
                },
                {
                ...
                }
        ]
    },
    {
    ...
    }
]}

我AP preciate甚至任何提示,公会我。谢谢!

I appreciate even any hints to guild me. Thanks!

推荐答案

更​​新,我发现了一个名模骨干提供模型之间和集合之间的关系。它已被证明是一个集合中的集合以及深嵌套模型数据的最佳解决方案。

Update, I found a SuperModel for backbone which provides relationships between models and between collections. It has proved to be a great solution for Collections within Collections as well as Deep Nested Model data.

模型是$ P $与他们通过钥匙等车型的关系对确定。在模型的初始化/解析在该键的JSON的任何值被传递给新的相关模型或集合。在两个模型/集之间建立的一种关系。

Models are pre-defined with their relationships to other models via key. During the initialize/parse of the model any values in the JSON at that key gets passed off to a new related model or collection. A relationship is created between the two models/collections.

这意味着上面的例子中,我们可以做这样的事情与我们的模型:

This means with the above example we can do something like this with our models:

var Author = Supermodel.Model.extend({});
var Post = Supermodel.Model.extend({});
var Comment = Supermodel.Model.extend({});

var Posts = Backbone.Collection.extend({
  model: function(attrs, options) {
    return Post.create(attrs, options);
  }
});
var Comments = Backbone.Collection.extend({
  model: function(attrs, options) {
    return Comment.create(attrs, options);
  }
});

Post.has().one('author', {
  model: Author,
  inverse: 'post'
}).many('comments', {
  collection: Comments,
  inverse: 'post'
});

//reverse relationships could also be setup

用法

var posts = new Posts( postsObject ); //where postsObject is an array of posts

//With SuperModel, we are able to navigate the related models
posts.first().comments();
posts.first().comments().author();
posts.last().author();

小提琴

工作实例中的jsfiddle

这篇关于映射到JSON Backbone.js的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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