首页 > 代码库 > (四)backbone - DEMO - 通信录

(四)backbone - DEMO - 通信录

DEMO介绍

是DEMO - User List 的扩展,增加查询

 

大体实现

创建Contact Model

 1 var Contact = Backbone.Model.extend({ 2     defaults: {   3         name: ‘小强‘,   4         email: ‘walker@dead.com‘   5     }, 6     // validate user name 7     validate: function(attrs,options) { 8         if (attrs.name == "") {   9           return "what‘s the name?";  10         };  11     },12     // for user search13     filter: function(query) {14         if (typeof(query) === ‘undefined‘ || query === null || query === ‘‘) return true;  15         query = query.toLowerCase();  16         return this.get(‘name‘).toLowerCase().indexOf(query) != -1 || this.get(‘email‘).toLowerCase().indexOf(query) != -1;  17     }18 });

创建Contact Collection

1 var Contacts = Backbone.Collection.extend({2     model: Contact,3     localStorage: new Store(‘my-contacts‘)  // 存至本地   4 });

 

(四)backbone - DEMO - 通信录