[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 ...
随机推荐
- bzoj 2555
暴力. 收获: 1.第一道后缀自动机,大概知道怎么写了,有一些原理性的东西还要理解. 2.计算right集合的大小 /***************************************** ...
- Dijkstra_Liu博客100篇祭
创建博客,有两年三个月了.今天,写了100篇随笔了,又正值我的15岁生日,还是值得纪念一下. 两年过去了,我从学习:队列.栈.模拟.背包慢慢地变成了:Tarjan.线段树.树剖. 我也从一个初一的天真 ...
- 使用jQuery的插件qrcode生成二维码(静态+动态生成)及常见问题解决方法
一.简介 1.说明 qrcode其实是通过使用jQuery实现图形渲染,画图,支持canvas(HTML5)和table两种方式,您可以到https://github.com/jeromeetienn ...
- python开发_tkinter_图片操作
在java的swing中,我们可以找到一些有关图片的操作,对于python的tkinter类似,也有对于图片的相关操作 下面是我做的demo 运行效果: ======================= ...
- HTML5学习笔记4
10.表单元素表单元素用于获取用户的输入数据form 表示HTML表单属性: action 表示表单提交的页面 method 表示表单提交的请求方式:有POST和GET两种,默认GET(P ...
- C#程序集系列13,如何让CLR选择不同版本的程序集
本篇主要体验,在存在多个版本程序集的情况下,如何让CLR选择哪个版本程序集运行,以及程序集版本的切换. 分别生成非强名称程序集不同版本 □ 生成某个版本的程序集 →清理F盘as文件夹,剩下如下文件 → ...
- CentOS6.5和RedHat6.5下以rpm方式安装mysql-5.6.20
转帖;http://blog.csdn.net/mw08091020/article/details/39234207 a.检查下linux是不是已经安装了mysql rpm -qa | grep - ...
- 如何在发型不乱的前提下应对单日十亿计Web请求
原文地址:http://developer.51cto.com/art/201502/464640.htm 就在不久之前,AppLovin移动广告平台的单一广告请求数量突破了200亿大关——相当于每一 ...
- ie不支持max-height的解决之法
.div{ max-height: 100px; _height:expression(this.scrollHeight > 100 ? "100px" : "a ...
- cocos2d-x avdrid 试例
今天将cocos2d-x的示例项目tests编译到android真机运行,以及如何创建cocos2d-x的android项目. 打开cocos2d-x的tests项目,路径为:D:\cocos2d-x ...