A tiny and simple model and collection API.
- Models act like plain old objects (that are observable for mutations)
- Collections act like plain old arrays (that are observable for mutations)
- Zero dependencies
- Requires native ES2015 runtime for full feature set (needs
Proxyfor collection length)
- Collections implement a stable sort
- Similar in spirit to Backbone.Model and Backbone.Collection (or
Object.observe), but designed with ES2015 in mind
- v4.0.0 Library size: 11,909 bytes minified (2,251 bytes gzip-compressed)
- Typescript friendly, works well with
npm install @srhazi/simplemodel
;;;;;;// -> models are observable by property namesquirtle.on'name',;// * Collections are observable for adds/removes/resets/sortslineup.on'add',;lineup.on'remove',;// * Collections that hold Models are also observable for model changeslineup.on'change',;// * Models act just like objectssquirtle.name = squirtle.name + ', the destroyer';// "squirtle's name changed from <squirtle> to <squirtle, the destroyer>"// "squirtle, the destroyer (pokemon 1) name changed from <squirtle> to <squirtle, the destroyer>"// * Why two logs? There's a 'name' handler on the model and 'change' handler on the collection.// * Collections act just like arrays (though index access is typesafe)lineup!.level += 1;// "pikachu (pokemon 0) level changed from <17> to <18>!"// collections can be added tolineup.addcharmander;// log: charmander added to the group at index 2!// models can be updated atomicallycharmander.update;// log: charmeleon's level changed from <15> to <16>!// log: charmeleon's name changed from <charmander> to <charmeleon>!// collections can be removedlineup.removelineup!;// log: squirtle, the destroyer removed from the group! (was index 0)
- Note: Minified standalone build depends on closure-compiler.
- typescript version: 4.0.2
- node version: v12.16.2
- amdclean version: 2.7.0
- Closure Compiler version: v20200517 (Built on: 2020-05-18 22:36)
- Note: Version
4.0.0compatible with TypeScript version 3.0.3+
- Note: Version
3.0.1has tests which rely on TypeScript 2.8 features (conditional types). The library should build fine in earlier versions (back to at least 2.6), but tests will not build.
git clone https://github.com/sufianrhazi/simplemodel.git
// A model is an object that has some additional behavior;
A collection behaves almost like arrays that also have observable behavior. There are two types of collections:
makeSimpleCollection<T>(items: T, cmp: (a: T, b: T) => number): SimpleCollection<T>- observable (and sorted) list of any type of objects
makeModelCollection<T>(items: Model<T>, cmp: (a: T, b: T) => number): ModelCollection<T>- observable (and sorted) list of model objects
In addition to
ModelCollection<T> objects also emit
change events when a particular model within it changes.
There are a few quirks to collections:
- A collection may be sorted by providing a comparison function upon creation
- Sort order is preserved upon addition. Newly added items will be inserted in sort order
- If items are mutated, sort order is not preserved, you must call
.sort()to re-sort the collection.
- The comparison function can be set and cleared via calling
- If sorted, inserts are performed via binary search, which adds
O(lg(n))comparisons in addition to the overhead of resizing the underying array via
The following methods typical in arrays are not implemented:
// A collection is an array that has some additional behavior