[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));
}
});
[Backbone] Parse not formatted JSON code的更多相关文章
- java开发客户端发送请求到服务器端出现这样:JSON parse error: Unexpected character ('}' (code 125)): was expecting
org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unexpected cha ...
- JavaScript -- JSON.parse 函数 和 JSON.stringify 函数
JavaScript -- JSON.parse 函数 和 JSON.stringify 函数 1. JSON.parse 函数: 使用 JSON.parse 可将 JSON 字符串转换成对象. &l ...
- [Backbone] Parse JSON on Collection
// Get /appointments{ "per_page": 10, "page": 1, "total": 50, "ap ...
- How parse REST service JSON response
1. get JSON responses and go to : http://json2csharp.com/ 2. write data contracts using C# All class ...
- 使用JSON.parse()转化成json对象需要注意的地方
http://blog.csdn.net/u011277123/article/details/53055479 有三种方法: var str = '{"name":"小 ...
- Newtonsoft.Json code
序列化 Product product = new Product(); product.ExpiryDate = new DateTime(2008, 12, 28); JsonSerializer ...
- Ionic 微信支付
1.安装插件 ionic plugin add https://github.com/mrwutong/cordova-qdc-wxpay.git 2.代码 controller.js angular ...
- [待解决]报错:JSON parse error: Unexpected character
{"code":"9999","message":"JSON parse error: Unexpected character ...
- JSON.parse
摘自:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse The J ...
随机推荐
- String StringBuffer stringbuilder 区别
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 字符串类 ,长度不可变. 字符串缓存器类, 长度可变, 线程安全, 字符串构造器类,长度 ...
- 【20181102T2】飞越行星带【智商题+最小瓶颈路】
题面 [正解] 一眼不可做啊 --相当于求路线上穿过的点最小距离最大 最小最大--二分啊 现在相当于给一个直径,要判断这个直径是否能从左边穿到右边 我们可以在距离不超过直径的点连一条边,\(y=0\) ...
- 【推导】【贪心】Codeforces Round #472 (rated, Div. 2, based on VK Cup 2018 Round 2) D. Riverside Curio
题意:海平面每天高度会变化,一个人会在每天海平面的位置刻下一道痕迹(如果当前位置没有已经刻划过的痕迹),并且记录下当天比海平面高的痕迹有多少条,记为a[i].让你最小化每天比海平面低的痕迹条数之和. ...
- wikioi 1380 没有上司的舞会 树形dp
1380 没有上司的舞会 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description Ural大学有N个职员,编号为1~N.他 ...
- Jmeter+JDK的安装学习笔记
第一步:首先从jmeter的官网下载jmeter,目前最新版本为3.3,支持的JDK最高为1.8 下载地址: jmeter:http://jmeter.apache.org/download_jmet ...
- Any way to start Google Chrome in headless mode?
Any way to start Google Chrome in headless mode? - Stack Overflow Any way to start Google Chrome in ...
- ARM ® and Thumb ®-2 指令系统
指令表关键词 Rm {, <opsh>} 寄存器移位方式,将寄存器的移位结果作为操作数而Rm值保持不变 <Operand2> 灵活的使用第二个操作数. ...
- linux下patch命令使用详解---linux打补丁命令
http://blog.csdn.net/pashanhu6402/article/details/51849354 语 法:patch [-bceEflnNRstTuvZ][-B <备份字首字 ...
- 清理IIS Express上的网站
默认情况下,当使用Visual Studio浏览网页时,网站会被保存在IIS Express上,这些网站需要手动清理.可以通过命令行或界面进行清理. □ 通过命令行 →找到appcmd.exe在C:\ ...
- 使用EF Model First创建edmx模型,数据库有数据的情况下,如何同时更新模型和数据库
使用"EF Model First",我们可以很直观地在edmx文件中创建数据模型,并根据模型生成数据库,整个过程快速而高效.可当数据库中有了一定的数据量,同时,可能需要对模型中字 ...