1 // ========================================================================== 2 // Project: The M-Project - Mobile HTML5 Application Framework 3 // Copyright: (c) 2010 M-Way Solutions GmbH. All rights reserved. 4 // Creator: Sebastian 5 // Date: 04.11.2010 6 // License: Dual licensed under the MIT or GPL Version 2 licenses. 7 // http://github.com/mwaylabs/The-M-Project/blob/master/MIT-LICENSE 8 // http://github.com/mwaylabs/The-M-Project/blob/master/GPL-LICENSE 9 // ========================================================================== 10 11 m_require('core/foundation/model.js'); 12 13 /** 14 * @class 15 * 16 * The root object for RecordManager. 17 * 18 * A RecordManager is used by a controllers and is an interface that makes it easy for him to 19 * handle his model records. 20 * 21 * @extends M.Object 22 */ 23 M.RecordManager = M.Object.extend( 24 /** @scope M.RecordManager.prototype */ { 25 /** 26 * The type of this object. 27 * 28 * @type String 29 */ 30 type: 'M.RecordManager', 31 32 /** 33 * Array containing all currently loaded records. 34 * 35 * @type Object 36 */ 37 records: [], 38 39 /** 40 * Add the given model to the modelList. 41 * 42 * @param {Object} record 43 */ 44 add: function(record) { 45 this.records.push(record); 46 }, 47 48 /** 49 * Concats an array if records to the records array. 50 * 51 * @param {Object} record 52 */ 53 addMany: function(arrOfRecords) { 54 55 if(_.isArray(arrOfRecords)){ 56 this.records = this.records.concat(arrOfRecords); 57 58 } else if(arrOfRecords.type === 'M.Model') { 59 this.add(arrOfRecords); 60 } 61 62 }, 63 64 /** 65 * Resets record list 66 */ 67 removeAll: function() { 68 this.records.length = 0; 69 }, 70 71 /** 72 * Deletes a model record from the record array 73 * @param {Number} id The internal model id of the model record. 74 */ 75 remove: function(id) { 76 if(!id) { 77 M.Logger.log('No id given.', M.WARN); 78 return; 79 } 80 if(typeof(id) === 'string') { 81 id = parseInt(id); 82 } 83 rec = this.getRecordForId(id); 84 if(rec) { 85 this.records = _.select(this.records, function(r){ 86 return r.id !== rec.id; 87 }); 88 } 89 }, 90 91 /** 92 * Returns a record from the record array identified by the interal model id. 93 * @param {Number} id The internal model id of the model record. 94 */ 95 getRecordForId: function(id) { 96 var record = _.detect(this.records, function(r){ 97 return r.id === id; 98 }); 99 return record; 100 }, 101 102 /** 103 * Debug method to print out all content from the records array to the console. 104 * @private 105 */ 106 dumpRecords: function() { 107 _.each(this.records, function(rec){ 108 console.log(rec.id); 109 }); 110 } 111 112 });