[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.id
later 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 ...
随机推荐
- php最简单最基础入门笔记
偶然翻到之前刚学php时记录的笔记,特此分享给大家,希望对初学者有所帮助. php网页命名不支持中文 isset($abc) 判断变量是否被定义 empty($abc) 判断变量是否为空 u ...
- 保存全局Crash报告
CrashHandler.java UncaughtException处理类,当程序发生Uncaught异常的时候,有该类来接管程序,并记录发送错误报告 package com.amanda;imp ...
- ZOJ 2112 Dynamic Rankings (动态第k大,树状数组套主席树)
Dynamic Rankings Time Limit: 10 Seconds Memory Limit: 32768 KB The Company Dynamic Rankings has ...
- Linux内核hlist数据结构分析
在内核编程中哈希链表hlist使用非常多,比方在openvswitch中流表的存储中就使用了(见[1]).hlist的表头仅有一个指向首节点的指针.而没有指向尾节点的指针,这样在有非常多个b ...
- Smali语法简单介绍
Smali语言其实就是Davlik的寄存器语言: Smali语言就是android的应用程序.apk通过apktool反编译出来的都有一个smali文件夹,里面都是以.smali结尾的文件,文件的展示 ...
- winform datagridview 打印
转载:http://www.cnblogs.com/Irving/archive/2012/10/12/2721666.html c#实现打印功能 http://www.cnblogs.com/zhc ...
- 用资源管理器右键编译 Visual Studio 解决方案文件
每次改动 VC 工程之后都要重新编译,每次 VS 又会生成调试数据库文件,很费时间,于是研究了一下如何在资源管理器中直接编译,还真发现了解决办法. 以下是适用 Visual Studio 2008 的 ...
- 使用EF Model First创建edmx模型,数据库有数据的情况下,如何同时更新模型和数据库
使用"EF Model First",我们可以很直观地在edmx文件中创建数据模型,并根据模型生成数据库,整个过程快速而高效.可当数据库中有了一定的数据量,同时,可能需要对模型中字 ...
- 浅谈Android RecyclerView
Android RecyclerView 是Android5.0推出来的,导入support-v7包即可使用. 个人体验来说,RecyclerView绝对是一款功能强大的控件. 首先总结下Recycl ...
- 将React Native集成至Android原生应用
将React Native集成至Android原生应用 Android Studio 2.1 Preview 4生成的空项目 react-native 环境 0.22.2 初次编译后apk有1.1M, ...