繁体   English   中英

来自服务器的带有JSON的主干解析模型

[英]backbone parse model with JSON from server

目前,我正在使用骨干网,在新模型上运行访存,并从服务器获得响应,我正在访存的模型中应包含其他模型和集合,返回的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属性也成为集合?

您将需要重写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;
}

暂无
暂无

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

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