可以查看http://www.css88.com/doc/backbone/

backbone与angular http://www.infoq.com/cn/articles/backbone-vs-angular/

http://blog.csdn.net/huangxin0102/article/details/50930772

1.

/**
* 模型 - Model
*/
var Note = Backbone.Model.extend({
defaults: {
title: '',
created_at: new Date()
},
initialize: function() {
console.log('新创建了一条笔记:' + this.get('title'));
this.on('change', function(model, options) {
console.log('属性的值发生了变化');
});
this.on('change:title', function(model, options) {
console.log('title 属性发生了变化');
});
this.on('invalid', function(model, error) {
console.log(error);
})
},
validate: function(attributes, options) {
if (attributes.title.length < 3) {
return '笔记的标题字符数要大于 3';
}
}
}); /**
* 视图 - View
*/
var NoteView = Backbone.View.extend({
tagName: 'li',
className: 'item',
attributes: {
'data-role': 'list'
},
template: _.template(jQuery('#list-template').html()), render: function() {
this.$el.html(this.template(this.model.attributes));
return this;
}
});
/**
* 集合 - Collection
*/
var NoteCollection = Backbone.Collection.extend({
model: Note,
initialize: function() {
this.on({
'add': function(model, collection, options) {
console.log('ID: ' + model.id + '模型添加到了集合里');
},
'remove': function(model, collection, options) {
console.log('ID: ' + model.id + '模型从集合里删除掉了');
},
'change': function(model, options) {
console.log('集合里的模型发生了变化');
}
});
}
});
//学习到的
/**
* var ss = new NoteCollection();
ss.add({id: 4, title: '西红柿炒鸡蛋的做法'});//可以这样子新添加一个
ss.add({id: 4, title: '西红柿炒鸡蛋的做法'},{merge:true});
还有remove,reset,pop,shift
set方法可以设置remove:false的
*/ /**
* 集合视图 - Collection View
*/
var NoteCollectionView = Backbone.View.extend({
tagName: 'ul',
initialize: function() {
this.collection.on('add', this.addOne, this);
this.render();
},
render: function() {
this.collection.each(this.addOne, this);
return this;
},
addOne: function(note) {
var noteView = new NoteView({model: note});
this.$el.append(noteView.render().el);
}
});
/**
* 测试
*/ var note1 = new Note({id: 1, title: '西红柿炒鸡蛋的做法'});
var note2 = new Note({id: 2, title: '周六参加朋友的婚礼'});
var note3 = new Note({id: 3, title: '晚上回家洗尿布'}); var noteCollection = new NoteCollection([note1, note2, note3]);
var noteCollectionView = new NoteCollectionView({collection: noteCollection});

backbone的路由

var NoteRouter = Backbone.Router.extend({
routes: {
'notes': 'index',
'notes/:id': 'show',
'login(/from/*from)':'login'//*表示不论后边有多少层路由
}, index: function() {
jQuery('#note_list').html(noteCollectionView.el);
console.log('笔记列表');
}, show: function(id) {
this.navigate('login/from/notes/'+id,{trigger,true});//trigger true表示可以出发方法
console.log('笔记:' + id);
var note = noteCollection.get(id);
var noteView = new NoteView({model: note});
jQuery('#note_list').html(noteView.render().el);
}
}); var noteRoute = new NoteRouter;
Backbone.history.start();

  

backbone点滴的更多相关文章

  1. js的点滴

    一些好的博客 http://www.cnblogs.com/coding4/p/7809063.html canvas http://www.cnblogs.com/coding4/p/5593954 ...

  2. Backbone源码解析(六):观察者模式应用

    卤煮在大概一年前写过backbone的源码分析,里面讲的是对一些backbone框架的方法的讲解.这几天重新看了几遍backbone的源码,才发现之前对于它的理解不够深入,只关注了它的一些部分的细节和 ...

  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的history管理SPA应用的url

    本文介绍如何使用backbone的history模块实现SPA应用里面的URL管理.SPA应用的核心在于使用无刷新的方式更改url,从而引发页面内容的改变.从实现上来看,url的管理和页面内容的管理是 ...

  5. Backbone中的model和collection在做save或者create操作时, 如何选择用POST还是PUT方法 ?

    Model和Collection和后台的WEB server进行数据同步非常方便, 都只需要在实行里面添加一url就可以了,backbone会在model进行save或者collection进行cre ...

  6. Backbone.js 中的Model被Destroy后,不能触发success的一个原因

    下面这段代码中, 当调用destroy时,backbone会通过model中的url,向服务端发起一个HTTP DELETE请求, 以删除后台数据库中的user数据. 成功后,会回调触发绑定到dest ...

  7. Backbone.js应用基础

    前言: Backbone.js是一款JavaScript MVC应用框架,强制依赖于一个实用型js库underscore.js,非强制依赖于jquery:其主要组件有模型,视图,集合,路由:与后台的交 ...

  8. Backbone事件模块及其用法

    事件模块Backbone.Events在Backbone中占有十分重要的位置,其他模块Model,Collection,View所有事件模块都依赖它.通过继承Events的方法来实现事件的管理,可以说 ...

  9. Backbone源码学习之extend

    extend函数在backbone大概就20来行代码包括注释,对学习Javascript中"类"的继承很是好的学习资料. 下面先贴出Backbone中的源码,其中涉及到unders ...

随机推荐

  1. Codeforces 1100 - A/B/C/D/E/F - (Undone)

    链接:https://codeforces.com/contest/1100 A - Roman and Browser - [暴力枚举] 题意:浏览器有 $n$ 个网页,编号 $1 \sim n$, ...

  2. 单片机小白应该如何学习stm32的一些实践心得!

    嵌入式搬砖道路上的大三狗一枚,撑死算个初学者吧.才学有限,下面仅仅是本人对STM32学习的一点心得与建议,希望对题主有帮助吧. 心得:本人当初学习STM32的时候有一些跟风的因素,自以为学的芯片越多就 ...

  3. (4.8)mysql备份还原——binlog查看工具之show binlog的使用

    (4.8)mysql备份还原——binlog查看工具之mysqlbinlog及show binlog的使用 关键词:show binlog,mysql binlog查看,二进制文件查看,binlog查 ...

  4. FastDFS的单点部署

    1  安装libfastcommon 注意:在Centos7下和在Ubuntu下安装FastDFS是不同的,在Ubuntu上安装FastDFS需要安装libevent,而外Centos上安装FastD ...

  5. Cocos Creator Slider(进度条)的三种实现

    实现原理: 方法一:动态计算,slider上增加背景图,根据滑动的进度动态计算背景图的大小:方法二:slider+progress,根据slider滑动的进度,动态改变progress的显示进度:方法 ...

  6. SQL Server 之 事务与隔离级别实例讲解

    SQL Server 之 事务与隔离级别实例讲解 SQL Server 实现了6个隔离级别来防止并发情况下,类似企图并发的访问或修改同一数据时问题的发生.本文将带你体验全部6个隔离级别.正如你接下来将 ...

  7. 213. House Robber II(动态规划)

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  8. Python 函数中,参数是传值,还是传引用?

    在 C/C++ 中,传值和传引用是函数参数传递的两种方式,在Python中参数是如何传递的?回答这个问题前,不如先来看两段代码. 代码段1: def foo(arg): arg = 2 print(a ...

  9. docker mysql 数据库乱码

    创建 mysql 时,需要加上编码,不然会乱码: docker run --name mysql01 -p : -e MYSQL_ROOT_PASSWORD=pwd123 -d mysql:5.5 - ...

  10. Quartz.net定时任务框架的使用

    一:Nuget添加Quartz.net和Topshelf 二:新建HelloJob类继承IJob public class HelloJob : IJob       {            pub ...