[Backbone] Customzing Backbone
Convert the AppointmentForm view below to use Mustache templating. Make sure you remember to change the <%= %> placeholders with Mustache's {{}} placeholders.
var AppointmentForm = Backbone.View.extend({
template: Mustache.compile('<form>' +
'<input name="title" type="text" value="{{title }}" />' +
'<input name="name" type="text" value="{{ name }}" /></form>'),
render: function(){
this.$el.html(this.template(this.model.toJSON()));
return this;
}
});
As you can see in our Appointment view below, we are rendering the list of possible dates an appointment could occur. possibleDates is an attribute on the Appointment model, and it's value is always an array of strings.
Convert the view below to use Mustache.
App.Views.Appointment = Backbone.View.extend({
template: Mustache.compile('<h2>{{ title }}</h2>' +
'Possible Dates: <ul>{{ #possibleDates }}' +
'<li>{{ . }}</li>' +
'{{ /possibleDates }}</ul>'),
render: function(){
this.$el.html(this.template(this.model.attributes));
return this;
}
});
possibleDates has changed from an Array of strings to an Array of objects with day and time properties. Update the Mustache template below to render the li tag with both day and time inside them, like so:
<li>Tuesday at 3:00pm</li>
App.Views.Appointment = Backbone.View.extend({
template: Mustache.compile('<h2>{{ title }}</h2>' +
'Possible Dates: <ul>{{#possibleDates}}' +
'<li>{{day}} at {{time}}</li>' +
'{{/possibleDates}}</ul>'),
render: function(){
this.$el.html(this.template(this.model.attributes));
return this;
}
});
Dr. Goodparts has instituted a new (possibly controversial) policy: Appointments can not be changed. The office can create appointments, but they will no longer be able to update them or delete them.
To comply with this new policy, modify the Appointment model's sync function below to only work on read andcreate, but not on delete and update.
App.Models.Appointment = Backbone.Model.extend({
sync: function(method, model, options){
if (method === "read" || method === "create"){
Backbone.sync(method, model, options);
}else {
console.log("Dr. Goodparts says no!");
}
}
});
Well that was quick. Dr. Goodparts has changed his mind and now wants to be able to update and delete appointments again. Unfortunately, the server team has defected to Dr. Jay Query and so we've had to use HTML5 localStorage to store our appointments.
Below we've started to implement the localStorage strategy, handling the "read" and "create" methods. In this challenge, write the code to handle the "delete" method.
App.Models.Appointment = Backbone.Model.extend({
sync: function(method, model, options){
options = options || {};
switch(method){
case 'delete':
var key = "Appointment-" + model.id;
localStorage.removeItem(key, JSON.stringify(model));
break;
case 'update':
break;
case 'create':
var key = "Appointment-" + model.id;
localStorage.setItem(key, JSON.stringify(model));
break;
case 'read':
var key = "Appointment-" + model.id;
var result = localStorage.getItem(key);
if(result){
result = JSON.parse(result);
options.success && options.success(result);
}else if(options.error){
options.error("Couldn't find Appointment with id=" + model.id);
}
break;
}
}
});
Fantastic! Now to finish it up, all you need to do is implement the update case below. Use setItem andJSON.stringify, just like in the create case.
App.Models.Appointment = Backbone.Model.extend({
sync: function(method, model, options){
options = options || {};
switch(method){
case "delete":
var key = "Appointment-" + model.id;
localStorage.removeItem(key);
break;
case "update":
var key = "Appointment-" + model.id;
localStorage.setItem(key, JSON.stringify(model));
break;
case "create":
var key = "Appointment-" + model.id;
localStorage.setItem(key, JSON.stringify(model));
break;
case "read":
var key = "Appointment-" + model.id;
var result = localStorage.getItem(key);
if(result){
result = JSON.parse(result);
options.success && options.success(result);
}else if(options.error){
options.error("Couldn't find Appointment with id=" + model.id);
}
break;
}
}
});
[Backbone] Customzing Backbone的更多相关文章
- [Backbone]Make Backbone Better With Extensions
Backbone is becoming wildly popular as a web application development framework. Along with this popu ...
- 【Backbone】 Backbone初探
前言 在此之前研究了一段React,但是不得不承认React.Vue等MVVM框架相对于原有的Jquery来说,简直是翻天覆地的不同.它们之间的差异不仅仅体现在框架思维的不同,而是ES5到ES6的编程 ...
- MVC、MVP、MVVM、Angular.js、Knockout.js、Backbone.js、React.js、Ember.js、Avalon.js、Vue.js 概念摘录
注:文章内容都是摘录性文字,自己阅读的一些笔记,方便日后查看. MVC MVC(Model-View-Controller),M 是指业务模型,V 是指用户界面,C 则是控制器,使用 MVC 的目的是 ...
- Backbone源码分析(一)
距离上一篇博客有一段时间了,期间各种琐事萦绕.最主要的一件是,当我差不多将整个dojo核心源码看完,惊讶的发现dojo1.*的设计以是老态龙钟之象,而我沉溺在dojo中太久,已经不知道前端世界变成了什 ...
- RequireJS与Backbone简单整合
前言 昨天我们一起学习了Backbone,最后做了一个备忘录的例子,说是做了不如说是看了下官方提供的例子,所以最终我感觉我们还是没能掌握Backbone,今天还得做个其它例子先. 然后前面也只是草草学 ...
- 浅谈HTML5单页面架构(二)——backbone + requirejs + zepto + underscore
本文转载自:http://www.cnblogs.com/kenkofox/p/4648472.html 上一篇<浅谈HTML5单页面架构(一)--requirejs + angular + a ...
- TodoMVC中的Backbone+MarionetteJS+RequireJS例子源码分析之二 数据处理
当我们使用jQuery时大部分时间是聚焦于Dom节点的处理,给Dom节点绑定事件等等:前端mvc框架backbone则如何呢? M-Model,Collection等,是聚焦于数据的处理,它把与后台数 ...
- TodoMVC中的Backbone+MarionetteJS+RequireJS例子源码分析之一
Marionette牵线木偶,Backbone是脊骨的意思,Marionette是基于Backbone做扩展库,可以理解为把脊骨骨架绑线扯着变成牵线木偶动起来哈哈,使backbone更易使用呵呵! 构 ...
- Django+Tastypie作后端,Backbone作前端的TodoMVC
TodoMVC是各种js框架入门的比较经典的例子,详细可查看github地址https://github.com/tastejs/todomvc 接着上篇文章, 1,先在github上把backbon ...
随机推荐
- CXF发布webservice
http://wenku.baidu.com/link?url=dTJpXcL0TXslGAYYC6SSOrPGvjyEb974ZGx9-0dymU32YDjxuP8DwlI1sFpPCGqu_ywW ...
- 在MacBook下安装http链接的性能测试工具httping
一.如果没有brew,先安装brew. 安装命令如下:curl -LsSf http://github.com/mxcl/homebrew/tarball/master | sudo tar xvz ...
- bzoj 3997 Dilworth定理
看到这道题感觉像是网络流,如果没有权值,可以用DAG最小路径覆盖,有权值,感觉可以求一个上下界最小可行流,但内存卡了....时间估计也悬. 正解要用到一些数学知识,这里梳理一下: 定义: 偏序关系: ...
- poj 3164
朱刘算法 步骤: 1.计算出每个点边权最小的边的权(如果除根以外有其他的点没有入边,则不存在最小树形图),并记下边的另一个端点(称其为这个点的前趋) 2.沿着每个点向上走,如果在走到根或环上的点之前, ...
- php输出mysqli查询出来的结果
php连接mysql我有文章已经写过了,这篇文章主要是介绍从mysql中查询出结果之后怎么输出的问题. 一:mysqli_fetch_row(); 查询结果:array([0]=>小王) 查询: ...
- java 盒子模型
http://www.cnblogs.com/xiaohuochai/tag/javascript%E6%80%BB%E7%BB%93/
- 光速 React
光速 React Vixlet 团队优化性能的经验教训 在过去一年多,我们 Vixlet 的 web 团队已经着手于一个激动人心的项目:将我们的整个 web 应用迁移到 React + Redux 架 ...
- C++ 模板的编译 以及 类模板内部的实例化
在C++中.编译器在看到模板的定义的时候.并不马上产生代码,仅仅有在看到用到模板时,比方调用了模板函数 或者 定义了类模板的 对象的时候.编译器才产生特定类型的代码. 一般而言,在调用函数的时候,仅仅 ...
- iOS appStore中的应用 实现升级功能
.h文件中 <UIAlertViewDelegate> .m文件中 #import "SBJson.h" //解析sbjson 数据 - (void)vi ...
- Derby安装,创建数据库,在Java程序中使用Derby
1,下载并安装Derby: 下载地址:http://db.apache.org/derby /derby_downloads.html,下载最新版本. 我用的是10.5.3.0. 解压缩到任意文件夹, ...