all files / montage/core/serialization/deserializer/ unit-deserializer.js

94.44% Statements 17/18
100% Branches 8/8
75% Functions 3/4
94.44% Lines 17/18
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                       15×   15×                                                 11×                          
var Montage = require("../../core").Montage;
 
var UnitDeserializer = Montage.specialize(/** @lends UnitDeserializer# */ {
    _context: {value: null},
 
    create: {
        value: function () {
            return new this();
        }
    },
 
    initWithContext: {
        value: function (context) {
            this._context = context;
 
            return this;
        }
    },
 
    _templatePropertyRegExp: {
        value: /^([^:]+)(:.*)$/
    },
 
    /**
     * A valid template property reference is one that references a component
     * that exists and has the format: @<component>:<property>.
     */
    isValidTemplatePropertyReference: {
        value: function (label) {
            var templateProperty = this._templatePropertyRegExp.exec(label);
 
            if (templateProperty) {
                var componentLabel = templateProperty[1];
 
                if (this._context.hasObject(componentLabel)) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }
    },
 
    getObjectByLabel: {
        value: function (label) {
            if (this._context.hasObject(label)) {
                // All labels that exist are immediately resolved into an object
                // even if they are a valid template property reference. This
                // "trick" can be used to speed up template property lookups.
                return this._context.getObject(label);
            } else if (this.isValidTemplatePropertyReference(label)) {
                // Ignore valid template property references that are not
                // available yet. This can happen during the instantiation of
                // the repetition's content.
                return null;
            } else {
                throw new Error("Object with label '" + label + "' was not found.");
            }
        }
    }
});
 
exports.UnitDeserializer = UnitDeserializer;