[转]backbone.js 示例 todos
本文转自:http://www.css88.com/doc/backbone/examples/todos/index.html
<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="utf-8">
<title>Backbone.js Todos</title>
<link rel="stylesheet" href="todos.css"/>
</head> <body> <div id="todoapp"> <header>
<h1>Todos</h1>
<input id="new-todo" type="text" placeholder="What needs to be done?">
</header> <section id="main">
<input id="toggle-all" type="checkbox">
<label for="toggle-all">Mark all as complete</label>
<ul id="todo-list"></ul>
</section> <footer>
<a id="clear-completed">Clear completed</a>
<div id="todo-count"></div>
</footer> </div> <div id="instructions">
Double-click to edit a todo.
</div> <div id="credits">
Created by
<br />
<a href="http://jgn.me/">Jérôme Gravel-Niquet</a>.
<br />Rewritten by: <a href="http://addyosmani.github.com/todomvc">TodoMVC</a>.
</div> <script src="../../test/vendor/json2.js"></script>
<script src="../../test/vendor/jquery.js"></script>
<script src="../../test/vendor/underscore.js"></script>
<script src="../../backbone.js"></script>
<script src="../backbone.localStorage.js"></script>
<script src="todos.js"></script> <!-- Templates --> <script type="text/template" id="item-template">
<div class="view">
<input class="toggle" type="checkbox" <%= done ? 'checked="checked"' : '' %> />
<label><%- title %></label>
<a class="destroy"></a>
</div>
<input class="edit" type="text" value="<%- title %>" />
</script> <script type="text/template" id="stats-template">
<% if (done) { %>
<a id="clear-completed">Clear <%= done %> completed <%= done == 1 ? 'item' : 'items' %></a>
<% } %>
<div class="todo-count"><b><%= remaining %></b> <%= remaining == 1 ? 'item' : 'items' %> left</div>
</script> </body>
</html>
// An example Backbone application contributed by
// [Jérôme Gravel-Niquet](http://jgn.me/). This demo uses a simple
// [LocalStorage adapter](backbone-localstorage.html)
// to persist Backbone models within your browser. // Load the application once the DOM is ready, using `jQuery.ready`:
$(function(){ // Todo Model
// ---------- // Our basic **Todo** model has `title`, `order`, and `done` attributes.
var Todo = Backbone.Model.extend({ // Default attributes for the todo item.
defaults: function() {
return {
title: "empty todo...",
order: Todos.nextOrder(),
done: false
};
}, // Toggle the `done` state of this todo item.
toggle: function() {
this.save({done: !this.get("done")});
} }); // Todo Collection
// --------------- // The collection of todos is backed by *localStorage* instead of a remote
// server.
var TodoList = Backbone.Collection.extend({ // Reference to this collection's model.
model: Todo, // Save all of the todo items under the `"todos-backbone"` namespace.
localStorage: new Backbone.LocalStorage("todos-backbone"), // Filter down the list of all todo items that are finished.
done: function() {
return this.where({done: true});
}, // Filter down the list to only todo items that are still not finished.
remaining: function() {
return this.where({done: false});
}, // We keep the Todos in sequential order, despite being saved by unordered
// GUID in the database. This generates the next order number for new items.
nextOrder: function() {
if (!this.length) return 1;
return this.last().get('order') + 1;
}, // Todos are sorted by their original insertion order.
comparator: 'order' }); // Create our global collection of **Todos**.
var Todos = new TodoList; // Todo Item View
// -------------- // The DOM element for a todo item...
var TodoView = Backbone.View.extend({ //... is a list tag.
tagName: "li", // Cache the template function for a single item.
template: _.template($('#item-template').html()), // The DOM events specific to an item.
events: {
"click .toggle" : "toggleDone",
"dblclick .view" : "edit",
"click a.destroy" : "clear",
"keypress .edit" : "updateOnEnter",
"blur .edit" : "close"
}, // The TodoView listens for changes to its model, re-rendering. Since there's
// a one-to-one correspondence between a **Todo** and a **TodoView** in this
// app, we set a direct reference on the model for convenience.
initialize: function() {
this.listenTo(this.model, 'change', this.render);
this.listenTo(this.model, 'destroy', this.remove);
}, // Re-render the titles of the todo item.
render: function() {
this.$el.html(this.template(this.model.toJSON()));
this.$el.toggleClass('done', this.model.get('done'));
this.input = this.$('.edit');
return this;
}, // Toggle the `"done"` state of the model.
toggleDone: function() {
this.model.toggle();
}, // Switch this view into `"editing"` mode, displaying the input field.
edit: function() {
this.$el.addClass("editing");
this.input.focus();
}, // Close the `"editing"` mode, saving changes to the todo.
close: function() {
var value = this.input.val();
if (!value) {
this.clear();
} else {
this.model.save({title: value});
this.$el.removeClass("editing");
}
}, // If you hit `enter`, we're through editing the item.
updateOnEnter: function(e) {
if (e.keyCode == 13) this.close();
}, // Remove the item, destroy the model.
clear: function() {
this.model.destroy();
} }); // The Application
// --------------- // Our overall **AppView** is the top-level piece of UI.
var AppView = Backbone.View.extend({ // Instead of generating a new element, bind to the existing skeleton of
// the App already present in the HTML.
el: $("#todoapp"), // Our template for the line of statistics at the bottom of the app.
statsTemplate: _.template($('#stats-template').html()), // Delegated events for creating new items, and clearing completed ones.
events: {
"keypress #new-todo": "createOnEnter",
"click #clear-completed": "clearCompleted",
"click #toggle-all": "toggleAllComplete"
}, // At initialization we bind to the relevant events on the `Todos`
// collection, when items are added or changed. Kick things off by
// loading any preexisting todos that might be saved in *localStorage*.
initialize: function() { this.input = this.$("#new-todo");
this.allCheckbox = this.$("#toggle-all")[0]; this.listenTo(Todos, 'add', this.addOne);
this.listenTo(Todos, 'reset', this.addAll);
this.listenTo(Todos, 'all', this.render); this.footer = this.$('footer');
this.main = $('#main'); Todos.fetch();
}, // Re-rendering the App just means refreshing the statistics -- the rest
// of the app doesn't change.
render: function() {
var done = Todos.done().length;
var remaining = Todos.remaining().length; if (Todos.length) {
this.main.show();
this.footer.show();
this.footer.html(this.statsTemplate({done: done, remaining: remaining}));
} else {
this.main.hide();
this.footer.hide();
} this.allCheckbox.checked = !remaining;
}, // Add a single todo item to the list by creating a view for it, and
// appending its element to the `<ul>`.
addOne: function(todo) {
var view = new TodoView({model: todo});
this.$("#todo-list").append(view.render().el);
}, // Add all items in the **Todos** collection at once.
addAll: function() {
Todos.each(this.addOne, this);
}, // If you hit return in the main input field, create new **Todo** model,
// persisting it to *localStorage*.
createOnEnter: function(e) {
if (e.keyCode != 13) return;
if (!this.input.val()) return; Todos.create({title: this.input.val()});
this.input.val('');
}, // Clear all done todo items, destroying their models.
clearCompleted: function() {
_.invoke(Todos.done(), 'destroy');
return false;
}, toggleAllComplete: function () {
var done = this.allCheckbox.checked;
Todos.each(function (todo) { todo.save({'done': done}); });
} }); // Finally, we kick things off by creating the **App**.
var App = new AppView; });
[转]backbone.js 示例 todos的更多相关文章
- 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.js的一些认识
backbone.js已经不是当前最流行的前端框架了,但是对于我而言,依然具有比较好的学习价值.虽然目前来说,react,vue等mvvm框架非常火热,但是感觉自身还不到去使用这种框架的层次.这些技术 ...
- [转]backbone.js template()函数
本文转自:http://book.2cto.com/201406/43974.html 本文所属图书 > Backbone.js实战 资深Web开发专家根据Backbone js最新版本撰写,对 ...
- [转]backbone.js 初探
本文转自:http://weakfi.iteye.com/blog/1391990 什么是backbone backbone不是脊椎骨,而是帮助开发重量级的javascript应用的框架. 主要提供了 ...
- 使用backbone.js、zepto.js和trigger.io开发HTML5 App
为了力求运行速度快.响应迅即,我们推荐使用backbone.js和zepto.js. 为了让这个过程更有意思,我们开发了一个小小的示例项目,使用CSS重置样式.Backbone.js和带转场效果的几个 ...
- 什么是 Backbone.js
Backbone.js 是一个在JavaScript环境下的 模型-视图-控制器 (MVC) 框架.任何接触较大规模项目的开发人员一定会苦恼于各种琐碎的事件回调逻辑.以及金字塔般的代码.而且,在传统的 ...
- backbone.js初探(转)
BackBone是JavaScript frameworks for creating MVC-like web applications,最近流行的用来建立单页面web application的工具 ...
- 【转】Backbone.js学习笔记(二)细说MVC
文章转自: http://segmentfault.com/a/1190000002666658 对于初学backbone.js的同学可以先参考我这篇文章:Backbone.js学习笔记(一) Bac ...
- 【转】Backbone.js学习笔记(一)
文章转自: http://segmentfault.com/a/1190000002386651 基本概念 前言 昨天开始学Backbone.js,写篇笔记记录一下吧,一直对MVC模式挺好奇的,也对j ...
随机推荐
- Use a cache
To create high-performance systems, sometimes you need to cache data. Play has a cache library and w ...
- 12款最佳的 WordPress 语法高亮插件推荐
语法高亮工具增强了代码的可读性,美化了代码,让程序员更容易维护.语法高亮提供各种方式由以提高可读性和文本语境,尤其是对于其中可以结束跨越多个页面的代码,以及让开发者自己的程序中查找错误.在这篇文章中, ...
- 由Vue引发的getter和setter思考
公司的新项目决定使用Vue.js来做,当我打印出Vue实例下的data对象里的属性时,发现了一个有趣的事情: 它的每个属性都有两个相对应的get和set方法,我觉的这是多此一举的,于是去网上查了查Vu ...
- Eclipse CDT Linux下内存分析 补记
常用工具汇总 http://www.ibm.com/developerworks/cn/linux/l-cn-memleak/ 常用的内存分析工具 http://en.wikipedia.org/wi ...
- Ida动态修改android程序的内存数据和寄存器数值,绕过so文件的判断语句
我们继续分析自毁程序密码这个app,我们发现该程序会用fopen ()打开/proc/[pid]/status这个文件,随后会用fgets()和strstr()来获取,于是我们在strstr()处下个 ...
- C语言中的变量
1. 计算机需要处理数据 2.数据需要保存在存储器上 3. 计算机只能识别0或者1的二进制数据 4.我们看到的,用到的所有数据在计算机中都是以二进制存储的 5.内存中的相同的01二进制数据,以不同的编 ...
- c中的基本运算
一. 算术运算 C语言一共有34种运算符,包括了常见的加减乘除运算 1. 加法运算+ l 除开能做加法运算,还能表示正号:+5.+90 2. 减法运算- l 除开能做减法运算,还能表示符号:-10.- ...
- YUM源的简介,配置与使用
A.yum 简介 yum,是Yellow dog Updater, Modified 的简称,是杜克大学为了提高RPM 软件包安装性而开发的一种软件包管理器.起初是由yellow dog 这一发行版的 ...
- javascript中,对于this指向的浅见
# this的指向在函数创建的时候确定不了.只有在执行的时候,才可以确定. ## 1 . 这里的this指向window window.fn(); 所以this.user是undefined func ...
- request,session,application
JSP 的3个内置对象request,session,application,其实都有一个作用域,这些对象内部有一个Map成员用于存放数据,比如session对象的setAttribute(key,v ...