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. 【BZOJ 3456】城市规划

    http://www.lydsy.com/JudgeOnline/problem.php?id=3456 设\(f(n)\)表示n个点有标号无向连通图的数目. dp:\(f(n)=2^{n\choos ...

  2. loj#2718. 「NOI2018」归程

    题目链接 loj#2718. 「NOI2018」归程 题解 按照高度做克鲁斯卡尔重构树 那么对于询问倍增找到当前点能到达的高度最小可行点,该点的子树就是能到达的联通快,维护子树中到1节点的最短距离 s ...

  3. 程序逻辑问题---Writeup

    原题地址:http://ctf5.shiyanbar.com/web/5/index.php 打开后是一处登陆界面 右键查看源代码 发现有一处txt文件 很明显就是程序的源代码 可以看到其中一句 $s ...

  4. 鸟哥的私房菜:Bash shell(三)-命令别名与历史指令

    一  命令别名设定: alias, unalias 命令别名是一个很有趣的东西,特别是你的惯用指令特别长的时候!还有, 增设预设的属性在一些惯用的指令上面,可以预防一些不小心误杀档案的情况发生的时候! ...

  5. hdu 2732 最大流 **

    题意:题目是说一个n*m的迷宫中,有每个格子有柱子.柱子高度为0~3,高度为0的柱子是不能站的(高度为0就是没有柱子)在一些有柱子的格子上有一些蜥蜴,一次最多跳距离d,相邻格子的距离是1,只要跳出迷宫 ...

  6. Subsets LeetCode总结

    Subsets 题目 Given a set of distinct integers, nums, return all possible subsets. Note: The solution s ...

  7. Trie树之C-实现

    title: Trie树之C++实现 comments: true date: 2016-10-02 16:59:54 categories: 算法 tags: Trie树 前言 之前写了一篇偏向于理 ...

  8. Codeforces Round #222 (Div. 1) A. Maze dfs

    A. Maze 题目连接: http://codeforces.com/contest/377/problem/A Description Pavel loves grid mazes. A grid ...

  9. cocos2d-x3.0 Slider

    .h #include "cocos2d.h" #include "cocos-ext.h" #include "ui/CocosGUI.h" ...

  10. 用最简单的例子理解对象为Null模式(Null Object Pattern)

    所谓的"对象为Null模式",就是要求开发者考虑对象为Null的情况,并设计出在这种情况下的应对方法. 拿"用最简单的例子理解策略模式(Strategy Pattern) ...