all files / montage/core/meta/ model.js

41.27% Statements 26/63
35.29% Branches 12/34
27.27% Functions 6/22
42.62% Lines 26/61
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380                                                                                                                                                                                                                                                                                       28×                     20× 20× 20×         20×                                                                                                                                                                                                                                                                                                                                                                         21×   21×                                        
var Montage = require("../core").Montage,
    ModelGroup = require("./model-group").ModelGroup,
    ObjectDescriptorModule = require("./object-descriptor"),
    deprecate = require("../deprecate");
 
    var _group = null;
 
/**
 * @class Model
 * @classdesc A Model represents a logical collection
 *
 * @extends Montage
 */
var Model = exports.Model = Montage.specialize( /** @lends Model.prototype # */ {
 
    /**
     * @constructs Model
     */
    constructor: {
        value: function Model() {
            this._name = null;
            this.modelModuleId = null;
            this.isDefault = false;
            this._objectDescriptorForPrototypeTable = {};
            return this;
        }
    },
 
    /**
     * @function
     * @param {string} name
     * @returns itself
     */
    initWithNameAndRequire: {
        value: function (name, _require) {
            Iif (!name) throw new Error("name is required");
            Iif (!_require) throw new Error("require is required");
 
            this._name = name;
            this._require = _require;
            Model.group.addModel(this);
            return this;
        }
    },
 
    serializeSelf: {
        value: function (serializer) {
            serializer.setProperty("name", this.name);
            if (this.objectDescriptors.length > 0) {
                serializer.setProperty("objectDescriptors", this.objectDescriptors);
            }
            serializer.setProperty("objectModelModuleId", this.modelInstanceModuleId);
        }
    },
 
    deserializeSelf: {
        value: function (deserializer) {
            this._name = deserializer.getProperty("name");
            //copy contents into the objectDescriptors array
            var value = deserializer.getProperty("objectDescriptors") || deserializer.getProperty("blueprints");
            if (value) {
                this._objectDescriptors = value;
            }
            this.modelInstanceModuleId = deserializer.getProperty("objectModelModuleId") || deserializer.getProperty("binderModuleId");
        }
    },
 
    _name: {
        value: null
    },
 
    /**
     * Name of the object.
     * The name is used to define the property on the object.
     * @function
     * @type {string}
     */
    name: {
        get: function () {
            return this._name;
        }
    },
 
    /**
     * @private
     */
    _require: {
        value: null
    },
 
    /**
     * Require for the binder.
     * All objectDescriptors added must be in this require's package, or in a direct
     * dependency.
     * @readonly
     * @returns {function} a package's `require` function
     */
    require: {
        get: function () {
            return this._require;
        }
    },
 
    /**
     * @private
     */
    _objectDescriptorForPrototypeTable: {
        value: null
    },
 
    /**
     * The identifier is the name of the binder and is used to make the
     * serialization of binders more readable.
     * @returns {string}
     */
    identifier: {
        get: function () {
            return [
                "objectModel",
                this.name.toLowerCase()
            ].join("_");
        }
    },
 
    /**
     * This is used for references only so that we can reload referenced
     * binders.
     */
    modelInstanceModuleId: {
        serializable:false,
        value: null
    },
 
    /**
     * Identify the default binder. Do not set.
     * @readonly
     * @type {boolean}
     */
    isDefault: {
        serializable: false,
        value: false
    },
 
    _objectDescriptors: {
        value: null
    },
 
    /**
     * The list of objectDescriptors in this binder.
     * @readonly
     * @returns {Array.<ObjectDescriptor>}
     */
    objectDescriptors: {
        get: function () {
            return this._objectDescriptors || (this._objectDescriptors = []);
        }
    },
 
    /**
     * @function
     * @param {?ObjectDescriptor} objectDescriptor
     * @returns objectDescriptor
     */
    addObjectDescriptor: {
        value: function (objectDescriptor) {
            Eif (objectDescriptor !== null) {
                var index = this.objectDescriptors.indexOf(objectDescriptor);
                if (index < 0) {
                    Iif ((objectDescriptor.model !== null) && (objectDescriptor.model !== this)) {
                        objectDescriptor.model.removeObjectDescriptor(objectDescriptor);
                    }
                    this.objectDescriptors.push(objectDescriptor);
                    objectDescriptor.model = this;
                }
            }
            return objectDescriptor;
        }
    },
 
    /**
     * @function
     * @param {ObjectDescriptor} objectDescriptor
     * @returns objectDescriptor
     */
    removeObjectDescriptor: {
        value: function (objectDescriptor) {
            if (objectDescriptor !== null) {
                var index = this.objectDescriptors.indexOf(objectDescriptor);
                if (index >= 0) {
                    this.objectDescriptors.splice(index, 1);
                    objectDescriptor.model = null;
                }
            }
            return objectDescriptor;
        }
    },
 
    /**
     * @function
     * @param {string} name
     * @param {string} moduleID
     * @returns {ObjectDescriptor} The new objectDescriptor
     */
    addObjectDescriptorNamed: {
        value: function (name) {
            return this.addObjectDescriptor(new ObjectDescriptorModule.ObjectDescriptor().initWithName(name));
        }
    },
 
    /**
     * Return the object descriptor associated with this prototype.
     * @function
     * @param {string} prototypeName
     * @param {string} moduleId
     * @returns {?ObjectDescriptor} objectDescriptor
     */
    objectDescriptorForPrototype: {
        value: deprecate.deprecateMethod(void 0, function (prototypeName) {
            return this.objectDescriptorForName(prototypeName);
        }, "objectDescriptorForPrototype", "objectDescriptorForName")
    },
 
    /**
     *
     * @param {string} name
     * @returns {?ObjectDescriptor} if this model has an object descriptor
     * with the provided name.  Otherwise, returns null.
     */
    objectDescriptorForName: {
        value: function (name) {
            var objectDescriptors = this.objectDescriptors,
                objectDescriptor = null,
                length = objectDescriptors.length;
            for (var i = 0; i < length && !objectDescriptor; i++) {
                if (objectDescriptors[i].name === name) {
                    objectDescriptor = objectDescriptors[i];
                }
            }
            return objectDescriptor;
        }
    },
 
    /**
     * @private
     */
    _objectDescriptorObjectProperty: {
        value: null
    },
 
    /**
     * Return the object descriptor object property for this model.
     * This will return the default if none is declared.
     * @type {ObjectProperty}
     */
    ObjectProperty: {
        get: function () {
            if (!this._objectDescriptorObjectProperty) {
                this._objectDescriptorObjectProperty = Model.group.defaultObjectDescriptorObjectProperty;
            }
            return this._objectDescriptorObjectProperty;
        }
    },
 
    objectDescriptorModuleId: require("../core")._objectDescriptorModuleIdDescriptor,
    objectDescriptor: require("../core")._objectDescriptorDescriptor,
 
    /******************************************************************************
     * Deprecated Methods
     */
 
 
    /**
     * The list of object descriptors in this model.
     * @deprecated
     * @readonly
     * @returns {Array.<ObjectDescriptor>}
     */
    blueprints: {
        get: deprecate.deprecateMethod(void 0, function () {
            return this.objectDescriptors;
        }, "blueprints", "objectDescriptors")
    },
 
    /**
     * @deprecated
     * @function
     * @param {?ObjectDescriptor} objectDescriptor
     * @returns objectDescriptor
     */
    addBlueprint: {
        value: deprecate.deprecateMethod(void 0, function (blueprint) {
            return this.addObjectDescriptor(blueprint);
        }, "addBlueprint", "addObjectDescriptor")
    },
 
    /**
     * @deprecated
     * @function
     * @param {ObjectDescriptor} objectDescriptor
     * @returns objectDescriptor
     */
    removeBlueprint: {
        value: deprecate.deprecateMethod(void 0, function (blueprint) {
            return this.removeObjectDescriptor(blueprint);
        }, "removeBlueprint", "removeObjectDescriptor")
    },
 
    /**
     * @deprecated
     * @function
     * @param {string} name
     * @param {string} moduleID
     * @returns {ObjectDescriptor} The new objectDescriptor
     */
    addBlueprintNamed: {
        value: deprecate.deprecateMethod(void 0, function (name) {
            return this.addObjectDescriptorNamed(name);
        }, "addBlueprintNamed", "addObjectDescriptorNamed")
    },
 
    /**
     * @deprecated
     * Return the objectDescriptor associated with this prototype.
     * @function
     * @param {string} prototypeName
     * @param {string} moduleId
     * @returns {?ObjectDescriptor} objectDescriptor
     */
    blueprintForPrototype: {
        value: deprecate.deprecateMethod(void 0, function (prototypeName) {
            return this.blueprintForName(prototypeName);
        }, "blueprintForPrototype", "blueprintForName")
    },
 
    /**
     * @deprecated
     * @param {string} name
     * @returns {?ObjectDescriptor}
     */
    blueprintForName: {
        value: deprecate.deprecateMethod(void 0, function (name) {
            return this.objectDescriptorForName(name);
        }, "blueprintForName", "objectDescriptorForName")
    },
 
    blueprintModuleId: require("../core")._objectDescriptorModuleIdDescriptor,
    blueprint: require("../core")._objectDescriptorDescriptor
 
}, {
 
    /**
     * Returns the model group.
     * @returns {ModelGroup}
     */
    group: {
        get: function () {
            if (_group === null) {
                _group = new ModelGroup();
            }
            return _group;
        }
    },
 
    /******************************************************************************
     * Deprecated Methods
     */
 
    /**
     * @deprecated
     * Returns the model group.
     * @returns {ModelGroup}
     */
    manager: {
        get: deprecate.deprecateMethod(void 0, function () {
            return exports.Model.group;
        }, "Binder.manager", "Model.group")
    }
 
});