@mezzy/data
TypeScript icon, indicating that this package has built-in type declarations

0.1.180 • Public • Published

@mezzy/data

Usage

New attribute collections must be initialized at instantiation, with an object or array representing the desired schema for the collection, along with default values.

const schema:any = {
    label: "User",
    value: {
        name: "",
        age: -1,
        superpowers: [ Superpowers.NONE ],
        strength: 0.5
    }
};

const attributeHero:ISuperHero = new AttributeCollection(schema);

In addition to providing default values, this initialization allows the attribute to later be updated and validated against the schema.

attributeHero.update({ name: "Bernard", age: 53, superpowers: [ Superpowers.FREEZE_RAY ], eyeColor: #f00 });

In the case above, the eyeColor property will be ignored, since it wasn't specified in the initial schema.

Now, we can listen for changes to the attribute and respond accordingly.

attributeHero.strength.changed.listen((args:AttributeEventArgs<ISuperHero>) => {
    if (args.attribute.value <= 0) args.attribute.parent.stun();
});

Package Sidebar

Install

npm i @mezzy/data

Weekly Downloads

1

Version

0.1.180

License

ISC

Unpacked Size

356 kB

Total Files

193

Last publish

Collaborators

  • mezzy