      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:      05.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/utility/logger.js');
     12
     13 /**
     14  * @class
     15  *
     16  * The root object for ModelRegistry.
     17  *
     18  * Model Registry is a central point for all models to get their Global Unique Identifier,
     19  * which is important for storage (guid is primary key as default).
     20  *
     21  * @extends M.Object
     22  */
     23 M.ModelRegistry = M.Object.extend(
     24 /** @scope M.ModelRegistry.prototype */ {
     25
     26     /**
     27      * The type of this object.
     28      *
     29      * @type String
     30      */
     31     type: 'M.ModelRegistry',
     32
     33
     34     /**
     35      * An array containing objects that save the model's name and their next GUId.
     36      * Acts globally.
     37      * @type Array|Object
     38      */
     39     registry: [],
     40
     41     /**
     42      * Calculates the next ID for a model named by modelName.
     43      *
     44      * @param {String} modelName The name of the model, e.g. 'Person'.
     45      * @returns {Number} The next internal model id for the model identified by modelName parameter.
     46      */
     47     getNextId: function(modelName) {
     48         for(i in this.registry){
     49             if(this.registry[i].modelName === modelName){
     50                 this.registry[i].id = this.registry[i].id + 1;
     51                 localStorage.setItem(modelName, this.registry[i].id);
     52                 return this.registry[i].id;
     53             }
     54         }
     55     },
     56
     57     /**
     58      * Sets the id for a certain model.
     59      *
     60      * @param {String} modelName The name of the model, e.g. 'Person'.
     61      * @param {Number} id The id of the model, e.g. 1.
     62      */
     63     setId: function(modelName, id) {
     64         for(i in this.registry){
     65             if(this.registry[i].modelName === modelName){
     66                 this.registry[i].id = id;
     67             }
     68         }
     69     },
     70
     71     /**
     72      * Register a model in the registry.
     73      * Set nextGUId for this model to initial value 0.
     74      *
     75      * @param {String} modelName The name of the model, e.g. 'Person'.
     76      */
     77     register: function(modelName) {
     78
     79         if(_.detect(this.registry, function(m){ return m.modelName === modelName })) {
     80             return;
     81         }
     82
     83         var obj = {
     84             modelName: modelName,
     85             id: 0
     86         };
     87         this.registry.push(obj);
     88
     89     }
     90
     91 });