javascript - 来自服务器的带有 JSON 的 Backbone 解析模型

标签 javascript json parsing backbone.js

我目前正在使用 backbone,我在一个新模型上运行一个获取,并从服务器得到一个响应,我正在获取的模型中应该有其他模型和集合,返回的 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 中成为一个集合,同样我如何确保organization 对象的 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;
}

关于javascript - 来自服务器的带有 JSON 的 Backbone 解析模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31294776/

相关文章:

javascript - 将 JSON 字符串转换为 Javascript 数组

c - 如何在 C 中创建 APDU (TLV) 解析器

java - Eclipse JDT Java Parser 中是否有任何类型的 "line visitor"?如果没有,有人知道一个好的解决方法吗?

jquery - 如何获得正确的表格格式? Jquery问题

javascript - 如何以 HH :MM:SS? 格式在 JavaScript 中显示当前时间

javascript - Chartjs 柱上平均线

javascript - jQuery 按键不起作用

iphone - 添加小数点后的字典上的ios双引号

json - 如何通过mac终端导入json文件到mongo?

java - 什么是XML中的延迟节点扩展?