(二)backbone - demo - user list

Demo介绍

学习了如何基本的使用Backbone,展示用户信息

使用JQuery操作DOM,backbone.localStorage.js操作localstorage

大体实现

创建user Model

 1 var User = Backbone.Model.extend({
 2         defaults: {
 3             username: ‘小强‘
 4         },
 5         initialize: function() {
 6             if (!this.get("username")) {
 7                 this.set({"username": this.defaults.username});
 8             }
 9             if (!this.get("userid")) {
10                 this.set({"userid": ++userid});
11             }
12         }
13     });

创建user Collection管理users

1 var UserCollection = Backbone.Collection.extend({
2     model: User,
3     // 持久化到本地数据库
4     localStorage: new Store("users")
5 });

创建View

按功能页划分:

- 列表页

- 添加(修改)页

- 详情页

创建基础View

1 var View = Backbone.View.extend({
2     register:function (state) {
3         this.state = state;
4         return this;
5     }
6 });

创建User Item 视图

 1 var UserItemView = View.extend({
 2     tagName: ‘li‘,  // 放view展示内容的外层容器,默认div
 3     template: _.template($(‘#user-item-template‘).html()),
 4     render: function () {    // 渲染
 5         this.$el.html(this.template(this.model.toJSON()));
 6         return this;
 7     },
 8     events:{  // 事件列表
 9         ‘click .removeUser‘: ‘deleteUser‘,
10         ‘click .viewUser‘: ‘viewUser‘
11     },
12     viewUser: function() {
13         this.state.trigger(‘viewUser‘, this.model);
14     },
15     deleteUser: function() {
16         this.state.trigger(‘removeUser‘, this.model);
17         this.remove();
18     }
19 });

创建User list 视图,该视图可创建user item view

 1 var userListView = View.extend({
 2     template: _.template($(‘#list-template‘).html()),
 3     initialize: function() {
 4         var view = this;
 5         this.state = new Backbone.Model();  // 实例化一个model
 6         this.router = this.options.router;
 7         // 调用fetch触发reset
 8         this.collection.unbind(‘reset‘);
 9         this.collection.bind(‘reset‘, this.addAll, this);
10         setTimeout(function(){
11             view.collection.fetch();
12         },0);
13     },
14     render: function() {
15         var view = this;
16         this.$el.html(this.template(this.state.toJSON()));
17         // 服务器同步
18         this.state.on(‘removeUser‘, function(user) {
19             user.destory();
20             view.collection.remove(user);
21         });
22         this.state.on(‘viewUser‘, function(user) {
23             view.router.navigate(‘user/‘ + user.cid, {trigger:true});
24         });
25
26         return this;
27     },
28     createUserItemView: function(user) {
29         var userItemView = new UserItemView({
30             model: user
31         });
32         userid = Math.max.call(null, user.get(‘userid‘),userid);
33         userItemView.register(this.state).render().$el.appendTo($(‘#list‘));
34     },
35     addAll: function() {
36         this.collection.each(this.createUserItemView.bind(this));
37     }
38 }); 

User Modify 视图

 1 var UserModifyView = View.extend({
 2     template: _.template($(‘#modify-template‘).html()),
 3     initialize: function() {
 4         this.router = this.options.router;
 5     },
 6     render: function() {
 7         var view = this;
 8         if(this.model) {
 9             this.$el.html(this.template(this.model.toJSON()));
10         }
11         else {
12             this.$el.html(this.template({username: ‘‘}));
13         }
14         setTimeout(function() {
15             view.$el.find(‘input‘).focus().select();    //设置焦点并选中
16         }, 0);
17         return this;
18     },
19     events: {
20         ‘click a.add‘: ‘modify‘
21     },
22     modify: function(){
23         var view = this;
24         if(this.model){
25             this.model.save({‘username‘: this.$el.find(‘input‘).val()});
26         }
27         else {
28             this.router.userCollection.create(new User({
29                 username:view.$el.find(‘input‘).val(),
30                 userid: ++userid
31             }));
32         }
33         this.router.navigate(‘list‘, {trigger:true});   // 跳转至list
34     }
35 });

User Detail 视图

 1 var UserView = View.extend({
 2     template: _.template($(‘#user-template‘).html()),
 3     initialize: function(){
 4         this.router = this.options.router;
 5     },
 6     render: function(){
 7         var view = this;
 8         this.$el.html(this.template(this.model.toJSON()));
 9         return this;
10     },
11     events: {
12         ‘click .editUser‘: ‘editUser‘
13     },
14     editUser: function() {
15         this.router.navigate(‘edit/‘ + this.model.cid, {trigger:true});
16         this.remove();
17     }
18 });

Router 控制器

使用控制器将定义的类进行组合

 1 var App = Backbone.Router.extend({
 2     initialize: function(el) {
 3         this.el = el;
 4         this.userCollection = new UserCollection();
 5     },
 6     routes: {
 7         ‘‘: ‘list‘,
 8         ‘list‘: ‘list‘,
 9         ‘add‘: ‘edit‘,
10         ‘edit/:cid‘: ‘edit‘,
11         ‘user‘: ‘user‘,
12         ‘user:/:cid‘: ‘user‘
13     },
14     list: function() {
15         var router = this;
16         this.clean();
17         this.currentView = new UserListView({
18             collection: router.userCollection,
19             router:router
20         }).render().$el.appendTo($(this.el));
21     },
22     edit: function(cid) {
23         var router = this,
24             user = null;
25         this.clean();
26         if(cid){
27             user = router.userCollection.getByCid(cid);
28         }
29         this.currentView = new UserModifyView({
30             model:user,
31             router:router
32         }).render().$el.appendTo($(this.el));
33     },
34     user: function() {
35         var router = this,
36             user = null;
37         this.clean();
38         if(cid){
39             user = router.userCollection.getByCid(cid);
40         }
41         this.currentView = new UserView({
42             model:user,
43             router:router
44         }).render().$el.appendTo($(this.el));
45     },
46     clean:function () {
47         if (this.currentView) {
48             this.currentView.remove();
49             this.currentView = null;
50         }
51     }
52 });

•实例化App

1 new App(‘body‘);
2 Backbone.history.start();
时间: 2024-10-11 17:43:10

(二)backbone - demo - user list的相关文章

分享一个非常屌的eazyui二开demo

eazyui二开Demo非常吊,里面各种非常吊的样例,最喜欢的是 多文件进度条上传,一次可多选,还有流程,还有文本编辑器  非常简洁的 不像一些官网各种复杂的东西.主要为自己保留一份, 在线demo在这: http://jqext.sinaapp.com/# 为了防止人家收费就留了一份下载的 http://download.csdn.net/detail/qq873113580/8879415 上几个非常吊的效果图:

MVVM与Backbone demo

MVVM https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93viewmodel Model–view–view-model (MVVM) is a software architectural pattern. MVVM facilitates a separation of development of the graphical user interface – be it via a markup language or GU

分享一个很屌的eazyui二开demo

eazyui二开Demo很吊,里面各种很吊的例子,最喜欢的是 多文件进度条上传,一次可多选,还有流程,还有文本编辑器  很简洁的 不像一些官网各种复杂的东西,主要为自己保留一份, 在线demo在这: http://jqext.sinaapp.com/# 为了防止人家收费就留了一份下载的 http://download.csdn.net/detail/qq873113580/8879415 上几个很吊的效果图: 版权声明:本文为博主原创文章,未经博主允许不得转载.

(四)backbone - DEMO - 通信录

DEMO介绍 是DEMO - User List 的扩展,增加查询 大体实现 •创建Contact Model 1 var Contact = Backbone.Model.extend({ 2 defaults: { 3 name: '小强', 4 email: '[email protected]' 5 }, 6 // validate user name 7 validate: function(attrs,options) { 8 if (attrs.name == "") {

(五)backbone - DEMO - 通信录改造之使用requirejs

DEMO介绍是 DEMO通信录的扩展,使用requirejs模块化整合 大体实现 • model文件 model/contact.js 1 define(function (){ 2 // user contact 3 var Contact = (function(){ 4 var _c = Backbone.Model.extend({ 5 defaults: { 6 name: '小强', 7 email: '小强@无敌.com' 8 }, 9 // validate user name

RobotFrameWork接口报文测试-----(二)demo的升级版

在上一篇,简单的demo实现了讲xml的数据发送服务器端并取得recvi_buf,然后进行了简单的解析的操作.现在就要解决之前提过的2个问题: 1. 步骤这么多,难道每写一个脚本都要重复一次么? 2. 每个接口的sendbuf的xml的格式是不一样的,id不一样,里面的tag的nodename也是不一样的,这要怎么办?难道每个接口都给写一个createSendBuf么,肯定不是这样的,那要怎样去做? 针对第一个问题的解决,可以使用在RF内定义自己的Resource,然后写直接调用资源内定义的关键

Ace(二)Demo示例

Client: #include "ace/Log_Msg.h" #include "ace/OS.h" #include "ace/Service_Config.h" #include "ace/Event_Handler.h" #include "ace/Reactor.h" #include "ace/Svc_Handler.h" #include "ace/SOCK_S

RobotFrameWork接口报文测试-----(三)demo的加强版(数据驱动测试)

在上一篇RobotFrameWork接口报文测试-----(二)demo的升级版基础上,将接口的xml的格式保存在xml文件中,然后程序如果增加一个接口,在xml文件里添加即可,无需修改自动化测试里的其他模块,然后在工具加case就可以了,但是接口取值的数据全部都是直接在case里面录入的,也就是说,每增加一条测试用例,就需要在工具内添加一条case,测试数据始终都是在工具内控制,这让以前使用excel管理过测试数据的我感觉很不爽,总感觉得把数据放到excel内,然后实现数据驱动测试. 围绕着这

Backbone introduce

I had immersed in web developer more than one year. during this past year, I read a lot of books about fond-end. from css,javascript,jquery and html. especial books about javascript. Yes, Javascript one one lauguange which you perhaps love and hate t