zoukankan      html  css  js  c++  java
  • [Backbone] Parse not formatted JSON code

    The good Dr. recently had another team implement the server and they slightly messed up the format of the JSON returned for Appointment data. Instead of returning JSON like { "title": "Ms. Kitty Hairball Treatment", "cancelled": false, "id": 1 } the server is returning JSON like { "appointment": { "title": "Ms. Kitty Hairball Treatment", "cankelled": false, "identifier": 1 }

    Add to the parse function below code to handle return the JSON without the "appointment" as root.

    var Appointment = Backbone.Model.extend({
      parse: function(response){
        return response.appointment;
      }
    });

    Great! Now let's take care of that pesky spelling error.

    Change 'cankelled' to 'cancelled' and make sure to remove the 'cankelled' property.

    Make sure to refer to the JSON below and to the right.

    var Appointment = Backbone.Model.extend({
      parse: function(response){
        response = response.appointment;
        response.cancelled = response.cankelled;
        delete response.cankelled;
        return response;
      }
    });

    update the Appointment model to use the"identifier" string as the idAttribute instead of the default "id", that way you can call appointment.idlater on. 

    var Appointment = Backbone.Model.extend({
      idAttribute: 'identifier',
      parse: function(response){
        var appointment = response.appointment;
        appointment.cancelled = appointment.cankelled;
        delete appointment.cankelled;
        return appointment;
      }
    });

    In the Appointment instantiation code below, make sure the attributes get run through our new parse function by passing in the appropriate option to the Appointment constructor

    var appointment = new Appointment(data, {parse: true}); //Forse to pasre the data

    He just tried to create a new Appointment and it crashed the server because the JSON sent up to represent the Appointment was in the wrong format. Update toJSON below to return the JSON the server expects. (Make sure you don't modify the model.attributes object)

    var Appointment = Backbone.Model.extend({
      toJSON: function(){
        var attrs = _.clone(this.attributes);
        attrs.cankelled = attrs.cancelled;
        delete attrs.cancelled;
        return { appointment: attrs};
      }
    });

    Now that we've modified toJSON to return mangled JSON, we need to change our AppointmentView to useattributes instead of toJSON.

    var AppointmentView = Backbone.View.extend({
      template: _.template('<span>' +
                            '<%= title %></span>' +
                            '<a href="#">x</a>'),
    
      render: function(){
        this.$el.html(this.template(this.model.attributes));
      }
    });
  • 相关阅读:
    剑指 Offer 18. 删除链表的节点
    剑指 Offer 15. 二进制中1的个数
    剑指 Offer 11. 旋转数组的最小数字
    剑指 Offer 56
    剑指 Offer 10- II. 青蛙跳台阶问题
    剑指 Offer 10- I. 斐波那契数列
    剑指 Offer 09. 用两个栈实现队列
    剑指 Offer 06. 从尾到头打印链表
    C++ 异常机制
    读《大数据——互联网大规模数据挖掘与分布式处理》
  • 原文地址:https://www.cnblogs.com/Answer1215/p/3897524.html
Copyright © 2011-2022 走看看