標籤:des style blog http color 使用 os io
[來自:Backbone.js 開發秘笈 第3章]
Collection API
(function ($) { //define ----------------------------- var ModelD = Backbone.Model.extend({ defaults: { ID: 0, Name: ‘‘ }, idAttribute: ‘ID‘ }); //定義集合對象 /* 在其內部,模型被儲存在一個名為 models 的數組當中 */ var CollectionD = Backbone.Collection.extend({ model: ModelD }); //instance --------------------------- var modelInstance = new ModelD({ ID: 1, Name: ‘James‘ }); //執行個體化集合對象 var collectionInstance = new CollectionD([ { ID: 2, Name: ‘Yoyo‘ }, modelInstance ]); //apply ----------------------------- //擷取索引處的集合項目 var model2 = collectionInstance.at(1); //擷取模型索引值 var model2Index = collectionInstance.indexOf(modelInstance); //擷取集合長度 var count = collectionInstance.length; //擷取指定 ID 的模型 /* 通過其內部 _byId 索引值對集合進行查詢 */ var model1 = collectionInstance.get(2); //添加模型 [重複不會添加到集合中,觸發 add 事件] collectionInstance.add([{ ID: 3, Name: ‘Pepe‘ }], { at: 0//插入點 }); //刪除模型 collectionInstance.remove([3]); //重設模型 collectionInstance.reset([{ ID: 4, Name: ‘Ramos‘ }]); //以隊列或棧的形式操作 /* .push() .pop() .unshift() .shift() */ //排序 /* 註:定義 comparator 回呼函數後,插入值後會被調用來確保順序 */ collectionInstance.comparator = function (model) { return model.get(‘Name‘); }; //或 /* collectionInstance.comparator = function(m1, m2) { return m1.get(‘ID‘) - m2.get(‘ID‘); }; */ /* 模型被更新後,需手動調用 sort() 進行排序或綁定到 change 事件 */ collectionInstance.sort(); //過濾集合 var collection2 = collectionInstance.where({ Name: ‘Pepe‘ }); //遍曆集合 collectionInstance.each(function (mode, index, list) { }); //是否都滿足條件 var isMultiple = collectionInstance.every(function (model) { return model.get(‘ID‘) > 0; }); //是否任意滿足條件 var isOther = collectionInstance.some(function (model) { return mode.get(‘ID‘) === 0; }); //擷取集合指定屬性的值集合 var names = collectionInstance.pluck(‘Name‘); //擷取集合特定的值集合 var plusColl = collectionInstance.map(function (model) { return model.get(‘ID‘) + model.get(‘Name‘); }); //擷取歸併單一值 var plusID = collectionInstance.reduce(function (plusValue, model) { return (plusValue || 0) + model.get(‘ID‘); }, 0/*初始值*/); //集合的鏈式操作 /* chain() */ var plusID2 = collectionInstance.chain().pluck(‘ID‘).reduce(function (plusValue, value) { return plusValue + value; }, 0).value();})(jQuery);
外掛程式擴充:
1. NoSQL 查詢 more api
(function ($) { //依賴 backbone.query.js //https://github.com/davidgtonge/backbone_query //define -------------------------------------- var ModelQ = Backbone.Model.extend({ defaults: { id: 0, name: ‘‘, age: 0, rank: 1001 } }); //通過其基類進行外掛程式擴充 var CollectionQ = Backbone.QueryCollection.extend({ model: ModelQ }); //instance ---------------------------------------- var collectionInstance = new CollectionQ([ { id: 1, name: ‘cxls‘, age: 33, rank: 1999 }, { id: 2, name: ‘yln‘, age: 22, rank: 1003 }, { id: 3, name: ‘pp‘, age: 31, rank: 1899 }, { id: 1, name: ‘lms‘, age: 28, rank: 1899 } ]); //apply -------------------------------------------- //查詢 var qCollection = collectionInstance.query({ name: ‘cxls‘ }); /* 操作符 */ /* $equal === $ne !== $in 包含所有可能的數組的值 [1899,1999] $nin !$in $exists 是否存在 true or flase $has 是否存在 true or flase $and $or $nor !$or $not !$and */ qCollection = collectionInstance.query({ rank: { $equal: 1299 } }); qCollection = collectionInstance.query({ $and: { age: 31, rank: 1299 } }); //排序 var oCollection = collectionInstance.query({ sortBy: ‘name‘, order: ‘desc‘/* 預設 asc */ }); //分頁 var pCollection = collectionInstance.query({ rank: 1999 }, { limit: 10/*返回結果數組的前 N 個元素 (必填) */, offset: 1/* 跳過前 N 個結果項 */, page: 2,/* 指定結果頁 */ cache: true/* 是否快取資料 預設 false */ }); /* 註:緩衝後如有更新,則許手動同步緩衝,可以通過調用 resetQueryCache() 方法 */})(jQuery);View Code
2. 集合儲存不同類型模型
(function ($) { //依賴 backbone.chosen.js //https://github.com/asciidisco/backbone.Chosen //define -------------------------------------- var Model1 = Backbone.Model.extend({ getFullName: function () { return this.get(‘firstName‘) + this.get(‘lastName‘); } }); var Model2 = Backbone.Model.extend({ getFullName: function () { return this.get(‘name1‘) + this.get(‘name2‘); } }); /* Backbone.chosen 重寫 _prepareModel 方法,可根據映射(map)來選擇正確的模型對象 */ var CollectionMultiple = Backbone.Collection.extend({ model: { chosen: {//定義多類型 [還可以使用函數來對模型進行映射] attr: ‘type‘,//模型中的類型屬性名稱 defaults: Model1,//預設類型 map: {//類型名對應模型 model1: Model1, model2: Model2 } } } }); //instance ------------------------ var collectionM = new CollectionMultiple([ { firstName: ‘Yo‘, lastName: ‘Yo‘, type: ‘model1‘ }, { name1: ‘C‘, name2: ‘Yo‘, Ronaldo: ‘model2‘ } ]);})(jQuery);View Code
3. 一對多關聯的關係
(function ($) { //依賴 backbone.relational.js //https://github.com/pauluithol/backbone-relational //define ---------------------------------------------- var EmployeeModel = Backbone.RelationalModel.extend(); var EmployeeCollection = Backbone.Collection.extend({ model: EmployeeModel }); var SuperviserModel = Backbone.RelationalModel.extend({ relations: [{ type: Backbone.HasMany,//一對多類型枚舉 key: ‘employees‘, relatedModel: EmployeeModel, collectionType: EmployeeCollection,//集合類型 reverRelation: { key: ‘superviser‘ }, includeInJSON: [‘id‘, ‘name‘]//可控制匯出到 JSON 格式的屬性 }] }); //instance --------------------------------------- var superviserInstance = new SuperviserModel({ id: 1, name: ‘yoyo‘, employees: [ { id: 2, name: ‘abc‘ }, { id: 3, name: ‘edf‘ } ] }); //apply ------------------------------------------ var employee1Name = superviserInstance.get(‘employees‘).at(0).get(‘name‘); var supName = superviserInstance.get(‘employees‘).at(0).get(‘superviser‘).get(‘name‘); //添加對象或關係 [自動關聯一對多關聯性] superviserInstance.get(‘employees‘).add({ id: 5, name: ‘xyz‘ }); var newEmployee = new EmployeeModel({ id: 4, name: ‘ghi‘, superviser: superviserInstance }); //擷取 JSON 形式對象 var superJSON = superviserInstance.toJSON();})(jQuery);View Code