Javascript 使用来自服务器的JSON的主干解析模型

Javascript 使用来自服务器的JSON的主干解析模型,javascript,json,parsing,backbone.js,Javascript,Json,Parsing,Backbone.js,目前我正在使用主干网,我在一个新模型上运行一个fetch,并从服务器获得一个响应,我正在获取的模型中应该有其他模型和集合,返回的JSON支持这一点,它看起来像这样 { "id" : 230, "name" : "A project name", "briefversion":{ "id":199, "project_id":230, "version_number":1

目前我正在使用主干网,我在一个新模型上运行一个fetch,并从服务器获得一个响应,我正在获取的模型中应该有其他模型和集合,返回的JSON支持这一点,它看起来像这样

    {
        "id" : 230,
        "name" : "A project name",
        "briefversion":{
            "id":199,
            "project_id":230,
            "version_number":1,
            "name":"Version 1",
            "created_at":"2015-05-14 10:22:29",
            "updated_at":"2015-05-14 10:22:29",
            "briefversionsections":[{
                "id":947,
                "briefversion_id":199,
                "position":1,
                "name":"Overview",
                "content":"<p>A general description of the project and some background information, also worth including some context, where is the work going to be used? Billboards, online, showroom etc</p><div><img src="//www.sketchup.com/images/case_study/architecture/robertson_walsh_3.jpg"/></div>",
               "created_at":"2015-05-14 10:22:29",
               "updated_at":"2015-05-14 10:22:29",
               "briefsectonattachments":{}
        }, {
           "id":948,
           "briefversion_id":199,
           "position":2,
           "name":"Scope of work",
           "content":"<p>A list of the deliverables, e.g.</p><ul><li>An exterior view</li><li>An interior view</li><li>An animation</li><li>A website</li></ul>",
           "created_at":"2015-05-14 10:22:29",
           "updated_at":"2015-05-14 10:22:29",
           "briefsectonattachments":{}
        },{
           "id":949,
           "briefversion_id":199,
           "position":3,
           "name":"Target market",
           "content":"<p>ASCribe who the work is to appeal to, what are the demographics and end user types.</p>",
           "created_at":"2015-05-14 10:22:29",
           "updated_at":"2015-05-14 10:22:29",
           "briefsectonattachments":{
        }
      }]
    },
    "organisations":{
    "id":55,
    "name":"Jaguar",
    "uri_hash":"jaguar",
    "slug":"S336e056",
    "information":"",
    "type":"organisation",
    "currency":"USD",
    "notifications":"0",
    "add_all":"0",
    "created_at":"-0001-11-30 00:00:00",
    "updated_at":"2015-05-20 09:16:21",
    "users":[
    {
        "id":111,
        "email":"xxxxxxxx@gmail.com",
        "first_name":"Matty",
        "last_name":"Brook",
        "display_name":"mattybrook",
        "initials":"MB",
        "remember_me":null,
        "active":"1",
        "invite_code":null,
        "forgotten_code":null,
        "cost_visible":0,
        "login_type":"normal",
        "api_token":null,
        "created_at":"2015-03-16 15:49:58",
        "updated_at":"2015-05-15 13:12:45",
        "deleted_at":null,
        "pivot":{
            "organisation_id":55,
            "user_id":111,
            "is_admin":"0"
        }
    }
}
{
“id”:230,
“名称”:“项目名称”,
“简要版本”:{
“id”:199,
“项目id”:230,
“版本号”:1,
“名称”:“版本1”,
“创建时间”:“2015-05-14 10:22:29”,
“更新时间”:“2015-05-14 10:22:29”,
“简介版本章节”:[{
“id”:947,
“版本号”:199,
"立场":一,,
“名称”:“概述”,
“内容”:“项目的一般说明和一些背景信息,也值得包括一些背景,作品将在哪里使用?广告牌、在线、展示室等”

, “创建时间”:“2015-05-14 10:22:29”, “更新时间”:“2015-05-14 10:22:29”, “简介部分附件”:{} }, { “id”:948, “版本号”:199, "立场":二,, “名称”:“工作范围”, “内容”:“可交付成果列表,例如外部视图
  • 内部视图
  • 动画
  • 网站
    • ”, “创建时间”:“2015-05-14 10:22:29”, “更新时间”:“2015-05-14 10:22:29”, “简介部分附件”:{} },{ “id”:949, “版本号”:199, “立场”:3, “名称”:“目标市场”, “内容”:“说明该作品将吸引哪些人,有哪些人口统计数据和最终用户类型。

      ”, “创建时间”:“2015-05-14 10:22:29”, “更新时间”:“2015-05-14 10:22:29”, “简要说明附件”:{ } }] }, “组织”:{ “id”:55, “名称”:“捷豹”, “uri_hash”:“捷豹”, “slug”:“S336e056”, “资料”:“, “类型”:“组织机构”, “货币”:“美元”, “通知”:“0”, “全部添加”:“0”, “创建时间”:“-0001-11-30 00:00:00”, 更新日期:“2015-05-20 09:16:21”, “用户”:[ { “id”:111, “电子邮件”:xxxxxxxx@gmail.com", “名字”:“马蒂”, “姓氏”:“布鲁克”, “显示名称”:“mattybrook”, “缩写”:“MB”, “记住我”:空, “活动”:“1”, “邀请代码”:空, “遗忘代码”:空, “可见成本”:0, “登录类型”:“正常”, “api_令牌”:空, “创建时间”:“2015-03-16 15:49:58”, “更新时间”:“2015-05-15 13:12:45”, “已删除位置”:空, “支点”:{ “组织id”:55, “用户id”:111, is_admin:“0” } } }
  • 因此,在获取之后,我如何确保
    briefversion
    成为一个模型,并且在
    briefversionsections
    中成为一个集合,同样,我如何确保Organization对象的users属性也成为一个集合?

    您将需要重写以处理从您那里获取JSON的问题r服务器的正确格式。完成后,您可以实例化方法中某些属性的集合

    比如说

    initialize: function () {
        this.briefversionsections = new Backbone.Collection(this.briefversionsections);
        this.users =  new Backbone.Collection(this.users);
     },
    
    parse: function (response, options) {
      var myModel = response.briefversion;
      myModel.users= response.organisations.users 
      return myModel;
    }