Skip to content

Model.defineModelProp #143

@dwhieb

Description

@dwhieb
/**
 * A utility function to define a property on a class which must always be an instance of a certain class. Note that the private property (`#propName`) must be defined on the class first.
 * @param {Object}   object    The object to define the property on
 * @param {String}   propName  The name of the property to define on the object
 * @param {Function} ItemModel The class to use for this property. Every time this property is set, this model will be instantiated.
 * @return {Object}            Returns the original object with the new property added
 */
static defineModelProp(object, propName, ItemModel) {

  Object.defineProperty(object, propName, {
    configurable: true,
    enumerable:   true,
    get() {
      return this[`#${propName}`];
    },
    set(val) {
      this[`#${propName}`] = new ItemModel(val);
    },
  });

}

Metadata

Metadata

Assignees

Labels

coreCore models and utilitiesfeatureChanges that affect the API

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions