[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 ...
随机推荐
- 浅析SDWebImage
浅析SDWebImage 在日常的开发过程中,如果去优雅的访问网络的图片并去管理每个工程必须要面对的问题,如果想要在工程里面提供易用.简洁.方便管理的解决方案还是很有挑战的,毕竟还要兼顾图片文件的缓存 ...
- windows下整数溢出分析
0x00前言 整数溢出就是往存储整数的内存单位存放的数据大于该内存单位所能存储的最大值,整数溢出有时候间接导致缓冲区溢出.如JPEG溢出漏洞(MS04-028). 0x01整数溢出分类 整数溢出可以分 ...
- HDU 5692 Snacks bfs版本dfs序 线段树
Snacks 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5692 Description 百度科技园内有n个零食机,零食机之间通过n−1条路相互连 ...
- hdoj 5199 Gunner map
Gunner Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5199 D ...
- Codeforces Round #288 (Div. 2) E. Arthur and Brackets 贪心
E. Arthur and Brackets time limit per test 2 seconds memory limit per test 128 megabytes input stand ...
- CF 277.5 B.BerSU Ball 二分图的最大匹配 模版题
题意:求二分图的最大匹配数量 模版如下: //二分图匹配(匈牙利算法的DFS实现) //初始化:g[][]两边顶点的划分情况 //建立g[i][j]表示i->j的有向边就可以了,是左边向右边的匹 ...
- Java_如何等待子线程执行结束
工作中往往会遇到异步去执行某段逻辑, 然后先处理其他事情, 处理完后再把那段逻辑的处理结果进行汇总的产景, 这时候就需要使用线程了. 一个线程启动之后, 是异步的去执行需要执行的内容的, 不会影响主线 ...
- PowerDesigner导出表为Excel(转)
打开脚本运行器Ctrl+Shift+X 导出: '*************************************************************************** ...
- Word文档中的语法高亮显示代码
有时候我们程序员也需要在word文档里面显示代码,但是直接复制过去 不好看,格式也不太对,这里给大家分享一个Word文档中的语法高亮显示代码的方法 http://www.planetb.ca/synt ...
- The YubiKey -- COMPARISON OF VERSIONS
COMPARISON OF YUBIKEY VERSIONS BASICSTANDARD & NANO BASICEDGE & EDGE-N PREMIUMNEO & NE ...