我是backbone.js的新手。我想创建一个集合对象来从服务器检索json对象

时间:2013-12-24 11:41:19

标签: backbone.js backbone.js-collections

这是我从服务器获取的数据。如何将此JSON对象存储在我的主干脚本中并在浏览器中显示?请帮忙。

我如何构建模型或嵌套集合来检索此数据.. 我从服务器获得两种响应依赖于查询。 第一回复:

[
  {
    "groups": [
      {
        "groupname": "Group_all",
        "group": [
          {
            "displayname": "facebook",
            "monitortype": "URL",
            "responsetimereport": "value2",
            "availabilityreport": "value4"
          },
          {
            "displayname": "gmai",
            "monitortype": "URL",
            "responsetimereport": "value5",
            "availabilityreport": "value6"
          },
          {
            "displayname": "zoho",
            "monitortype": "URL",
            "responsetimereport": "value2",
            "availabilityreport": "value1"
          }
        ]
      }
    ]
  },
  {
    "monitor": []
  }
]

2.响应

[
  {
    "groups": []
  },
  {
    "monitor": [
      {
        "displayname": "facebook",
        "monitortype": "URL",
        "responsetimereport": "value2",
        "availabilityreport": "value1"
      }
    ]
  }
]

我写的是为了达到这个目的,我在这里粘贴

studentdb.Monitor = Backbone.Model.extend({

    initialize : function(){
        this.Displayname = this.get('displayname');
        this.Monitortype = this.get('monitortype');
        this.Responsetimereport = this.get('responsetimereport');
        this.Availabilityreport= this.get('availabilityreport');

    }
  });
studentdb.Monitors = Backbone.Collection.extend({ model : studentdb.Monitor });

studentdb.Group1 = Backbone.Model.extend({

     initialize : function(){
            this.Displayname = this.get('displayname');
            this.Monitortype = this.get('monitortype');
            this.Responsetimereport = this.get('responsetimereport');
            this.Availabilityreport= this.get('availabilityreport');

        }

  });
studentdb.Group1s = Backbone.Collection.extend({ model : studentdb.Group1 });

studentdb.Group_outer = Backbone.Model.extend({

    defaults :{Groupname:""},
    initialize : function(){

      this.outer_group = new studentdb.Group1s(this.get('group'));
      this.Groupname = this.get('groupname');
      this.outer_group.parent = this;


    }
  });
studentdb.Group_outers = Backbone.Collection.extend({ model : studentdb.Group_outer });

 studentdb.Overall = Backbone.Model.extend({
    initialize : function(){

      this.group_outer =new studentdb.Group_outers(this.get('groups'));
      this.monitors = new studentdb.Monitors(this.get('monitor'));

    }
  });
 studentdb.Final = Backbone.Collection.extend({ 

     url:'https://data.json',
     model : studentdb.Overall,


  });

2 个答案:

答案 0 :(得分:1)

基本上,您必须使用从服务器获取的数据填充集合,然后基于模板呈现视图。 相反,给你一个完整的解决方案,我认为你应该首先阅读一些关于骨干的教程:

答案 1 :(得分:0)

你可以发布你的收藏代码吗?

我担心你的json响应可能不能直接用在骨干集合中。

在那个JSON中,数组包含两个成员,第一个包含另一个只有一个成员的数组(包含groupName和group(实际上应该被称为组))

如果是'Groups'集合,则需要使用parse。

这里的文档:http://backbonejs.org/#Collection-parse

它应该处理你的响应并返回一组Group模型属性,如下所示:

[
      {
        "displayname": "facebook",
        "monitortype": "URL",
        "responsetimereport": "value2",
        "availabilityreport": "value4"
      },
      {
        "displayname": "gmai",
        "monitortype": "URL",
        "responsetimereport": "value5",
        "availabilityreport": "value6"
      },
      {
        "displayname": "zoho",
        "monitortype": "URL",
        "responsetimereport": "value2",
        "availabilityreport": "value1"
      }
    ]