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的更多相关文章

  1. [Backbone]Make Backbone Better With Extensions

    Backbone is becoming wildly popular as a web application development framework. Along with this popu ...

  2. 【Backbone】 Backbone初探

    前言 在此之前研究了一段React,但是不得不承认React.Vue等MVVM框架相对于原有的Jquery来说,简直是翻天覆地的不同.它们之间的差异不仅仅体现在框架思维的不同,而是ES5到ES6的编程 ...

  3. 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 的目的是 ...

  4. Backbone源码分析(一)

    距离上一篇博客有一段时间了,期间各种琐事萦绕.最主要的一件是,当我差不多将整个dojo核心源码看完,惊讶的发现dojo1.*的设计以是老态龙钟之象,而我沉溺在dojo中太久,已经不知道前端世界变成了什 ...

  5. RequireJS与Backbone简单整合

    前言 昨天我们一起学习了Backbone,最后做了一个备忘录的例子,说是做了不如说是看了下官方提供的例子,所以最终我感觉我们还是没能掌握Backbone,今天还得做个其它例子先. 然后前面也只是草草学 ...

  6. 浅谈HTML5单页面架构(二)——backbone + requirejs + zepto + underscore

    本文转载自:http://www.cnblogs.com/kenkofox/p/4648472.html 上一篇<浅谈HTML5单页面架构(一)--requirejs + angular + a ...

  7. TodoMVC中的Backbone+MarionetteJS+RequireJS例子源码分析之二 数据处理

    当我们使用jQuery时大部分时间是聚焦于Dom节点的处理,给Dom节点绑定事件等等:前端mvc框架backbone则如何呢? M-Model,Collection等,是聚焦于数据的处理,它把与后台数 ...

  8. TodoMVC中的Backbone+MarionetteJS+RequireJS例子源码分析之一

    Marionette牵线木偶,Backbone是脊骨的意思,Marionette是基于Backbone做扩展库,可以理解为把脊骨骨架绑线扯着变成牵线木偶动起来哈哈,使backbone更易使用呵呵! 构 ...

  9. Django+Tastypie作后端,Backbone作前端的TodoMVC

    TodoMVC是各种js框架入门的比较经典的例子,详细可查看github地址https://github.com/tastejs/todomvc 接着上篇文章, 1,先在github上把backbon ...

随机推荐

  1. centOS7 apache ssl证书安装配置

    背景说明:服务器是centOS7.4 七牛申请的免费ssl证书 默认apache是没有安装SSL模块的,所以需要安装,接着使用命令: yum install -y mod_ssl apache目录 / ...

  2. [BZOJ1758][WC2010]重建计划(点分治+单调队列)

    点分治,对于每个分治中心,考虑求出经过它的符合长度条件的链的最大权值和. 从分治中心dfs下去取出所有链,为了防止两条链属于同一个子树,我们一个子树一个子树地处理. 用s1[i]记录目前分治中心伸下去 ...

  3. Python的静态方法和类成员方法

    http://www.cnblogs.com/2gua/archive/2012/09/03/2668125.html Python的静态方法和类成员方法都可以被类或实例访问,两者概念不容易理清,但还 ...

  4. angular 自定义指令参数详解【转】【个人收藏用】

    restrict:指令在dom中的声明形式 E(元素)A(属性)C(类名)M(注释) priority优先级:一个元素上存在两个指令,来决定那个指令被优先执行 terminal:true或false, ...

  5. Towelroot v3.0版发布 将支持更多设备 Towelroot v3.0下载

    Towelroot虽然已经发布一段时间了,虽然所Towelroot可以一键ROOT很多设备,虽然它只有100多K.不过还是有一小部分的机型没办法ROOT成功的,也不知道什么原因.不过不用担心,Geoh ...

  6. makfile.am 和makefile.in 的使用

    参考 http://blog.csdn.net/vevenlcf/article/details/48134313 http://linux.chinaunix.net/techdoc/develop ...

  7. SpringMVC和Springboot的区别(网摘)

    spring boot 我理解就是把 spring spring mvc spring data jpa 等等的一些常用的常用的基础框架组合起来,提供默认的配置,然后提供可插拔的设计,就是各种 sta ...

  8. WebLogic Server 12c相对JBoss EAP 6的优势

    原文来自:https://blogs.oracle.com/middlewareplace/entry/why_should_you_choose_oracle 1.多数据中心部署和集群 WebLog ...

  9. 重载 UINavigationController 设置左侧返回按钮的文字为图片

    UINavigationController 导航栏控制器的左侧返回按钮如果需要设置成图片,仅使用系统的是无法实现的,需要重载系统的导航栏控制器,在控制器推出之前替换掉leftBarButtonIte ...

  10. 利用 FastCoding 将对象进行本地持久化

    FastCoding https://github.com/nicklockwood/FastCoding A faster and more flexible binary file format ...