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

[Backbone] Parse not formatted JSON code的更多相关文章

  1. java开发客户端发送请求到服务器端出现这样:JSON parse error: Unexpected character ('}' (code 125)): was expecting

    org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unexpected cha ...

  2. JavaScript -- JSON.parse 函数 和 JSON.stringify 函数

    JavaScript -- JSON.parse 函数 和 JSON.stringify 函数 1. JSON.parse 函数: 使用 JSON.parse 可将 JSON 字符串转换成对象. &l ...

  3. [Backbone] Parse JSON on Collection

    // Get /appointments{ "per_page": 10, "page": 1, "total": 50, "ap ...

  4. How parse REST service JSON response

    1. get JSON responses and go to : http://json2csharp.com/ 2. write data contracts using C# All class ...

  5. 使用JSON.parse()转化成json对象需要注意的地方

    http://blog.csdn.net/u011277123/article/details/53055479 有三种方法: var str = '{"name":"小 ...

  6. Newtonsoft.Json code

    序列化 Product product = new Product(); product.ExpiryDate = new DateTime(2008, 12, 28); JsonSerializer ...

  7. Ionic 微信支付

    1.安装插件 ionic plugin add https://github.com/mrwutong/cordova-qdc-wxpay.git 2.代码 controller.js angular ...

  8. [待解决]报错:JSON parse error: Unexpected character

    {"code":"9999","message":"JSON parse error: Unexpected character ...

  9. JSON.parse

    摘自:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse The J ...

随机推荐

  1. QT学习笔记5:QMouseEvent鼠标事件简介

    一.QMouseEvent的详细描述 首先请注意,Qt中的QMouseEvent一般只涉及鼠标左键或右键的单击.释放等操作,而对鼠标滚轮的响应则通过QWheeEvent来处理. QMouseEvent ...

  2. SlickOne敏捷开发框架介绍(一) -- 基于Dapper, Mvc和WebAPI 的快速开发框架

    前言:在两年前(最初发布时间:2013年1月9日(csdn),当前文章时间2015年11月10日),项目组推出了基于Dapper,Mvc和WebApi的快速开发框架,随着后续Slickflow产品的实 ...

  3. 【scrapy】使用方法概要(一)(转)

    [请初学者作为参考,不建议高手看这个浪费时间] 工作中经常会有这种需求,需要抓取互联网上的数据.笔者就经常遇到这种需求,一般情况下会临时写个抓取程序,但是每次遇到这种需求的时候,都几乎要重头写,特别是 ...

  4. UCS UTF UTF-7 UTF-8 UTF-16

    Unicode也是一种字符编码方法,不过它是由国际组织设计,可以容纳全世界所有语言文字的编码方案.Unicode的学名是"Universal Multiple-Octet Coded Cha ...

  5. 如何设置Android手机的sqlite3命令环境

    1.在PC环境下启动一个模拟器(不是手机) 2.查看模拟器 /systen/xbin是否有sqlite3命令 adb shell cd /system/xbin ls 3.把模拟器 /system/x ...

  6. 记录一次软件Bug发生的过程

    结束一周的紧张工作,难得的休息时光,坐在电脑前浏览博客.听听歌.看看大片,这也算是一种享受. 因为年度的开发任务已经开始了,所以最近会特别忙,新人的成长又没有想象中的好,经常在他们身上看到自己去年的影 ...

  7. JQUERY AJAX无刷新异步上传文件

    AJAX无刷新上传文件并显示 http://blog.csdn.net/gao3705512/article/details/9330637?utm_source=tuicool jQuery For ...

  8. 相比xib 使用代码编排view 的一个明显的好处就是可以更好地重复使用已有代码,减少代码冗余。

    相比xib 使用代码编排view 的一个明显的好处就是可以更好地重复使用已有代码,减少代码冗余.

  9. Gradle在Windows环境与Linux上配置有哪些不同?

    我的开发环境:Windows + Android Studio + Gradle 2.8 all + Jenkins 公司CI 服务器环境: Linux + Gradle 2.10 bin + Jen ...

  10. JAVA压缩解压ZIP文件,中文乱码还需要ANT.JAR包

    package zip; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStrea ...