This class is used to store attributes of one data model. It has the following benifits.

  • Get notification when value of one attributes is changed
  • Have one changed property to store all of changed values
  • Store all of states of one model in one key/value object and make it is easy to serialize/deserialize model and model changes

Actually implementation of this class is based on class Model in Backbone.js. However, Model class in Backbone is too heavy. So we implement it again based on source code of class Model in Backbone.js. Morever, we want to keep our library with less external dependencies and don't want to introduce depenedency on Backbone.js.

Type Parameters

Constructors

  • Create one object to store attributes. For performance reason, values of attributes passed to constructor will not be cloned to attributes property. So it means that value of attributes property in this object is just a reference to arguments attributes passed to constructor.

    Type Parameters

    Parameters

    • Optionalattributes: Partial<T>

      Input attributes to store in this object

    • OptionaldefaultAttrs: Partial<T>

    Returns AcCmObject<T>

Properties

attributes: Partial<T>
changed: Partial<T>

Methods

  • Return an object containing all the attributes that have changed. Useful for determining what parts of a view need to be updated and/or what attributes need to be persisted to the server.

    Unset attributes will be set to undefined. You can also pass an attributes object to diff against the model, determining if there would be a change.

    Parameters

    • Optionaldiff: Partial<T>

    Returns Partial<T>

  • Create a new model with identical attributes to this one.

    Returns AcCmObject<T>

  • Gets the value of an attribute.

    For strongly-typed access to attributes, use the get method privately in public getter properties.

    Type Parameters

    • A extends string

      The key type extending string keys of T.

    Parameters

    • key: A

      The attribute key to retrieve.

    Returns undefined | T[A]

    The attribute value or undefined if not set.

    // Get a single attribute value
    const name = obj.get('name')
    const visible = obj.get('visible')

    // Check if attribute exists
    if (obj.get('name') !== undefined) {
    console.log('Name is set')
    }

    // For strongly-typed subclasses
    get name(): string {
    return super.get("name")
    }
  • Determine if the model has changed since the last "change" event. If you specify an attribute name, determine if that attribute has changed.

    Parameters

    Returns boolean

  • Get the previous value of an attribute, recorded at the time the last "change" event was fired.

    Type Parameters

    • A extends string

    Parameters

    • key: A

    Returns undefined | null | T[A]

  • Get all of the attributes of the model at the time of the previous "change" event.

    Returns Partial<T>

  • Sets one or more attributes on the object.

    For strongly-typed assignment of attributes, use the set method privately in public setter properties. Triggers change events unless the silent option is specified.

    Type Parameters

    • A extends string

      The key type extending string keys of T.

    Parameters

    • key: A

      The attribute key or an object of key-value pairs.

    • Optionalval: T[A]

      The value to set or options when key is an object.

    • Optionaloptions: AcCmObjectOptions

      Options for the set operation.

    Returns this

    The current instance for method chaining.

    // Set a single attribute
    obj.set('name', 'MyEntity')

    // Set multiple attributes
    obj.set({ name: 'MyEntity', visible: true })

    // Set with options
    obj.set('name', 'MyEntity', { silent: true })

    // Unset an attribute
    obj.set('name', undefined, { unset: true })

    // For strongly-typed subclasses
    set name(value: string) {
    super.set("name", value)
    }
  • Sets one or more attributes on the object.

    For strongly-typed assignment of attributes, use the set method privately in public setter properties. Triggers change events unless the silent option is specified.

    Parameters

    • key: Partial<T>

      The attribute key or an object of key-value pairs.

    • Optionaloptions: AcCmObjectOptions

      Options for the set operation.

    Returns this

    The current instance for method chaining.

    // Set a single attribute
    obj.set('name', 'MyEntity')

    // Set multiple attributes
    obj.set({ name: 'MyEntity', visible: true })

    // Set with options
    obj.set('name', 'MyEntity', { silent: true })

    // Unset an attribute
    obj.set('name', undefined, { unset: true })

    // For strongly-typed subclasses
    set name(value: string) {
    super.set("name", value)
    }