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