来自服务器

时间:2015-07-08 13:51:57

标签: javascript json parsing backbone.js

我目前正在使用骨干网,我在新模型上运行获取,并从服务器获得响应,我正在获取的模型应该包含其他模型和集合,返回的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"
        }
    }
}

因此,在获取之后,我如何确保briefversion成为模型并且在briefversionsections内成为一个集合,同样如何确保组织对象的users属性也成为一个集合?

1 个答案:

答案 0 :(得分:2)

您需要覆盖parse以便以正确的格式处理从服务器获取JSON。完成后,您可以为initialize方法中的某些属性实例化集合。

例如

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;
}