👯 SuperModel.js 👯
Harness the power of proxies!
- +: no depencies
- +: small size, SuperModel.min.js is ~4.5kb uncompressed
- -: only works in environments supporting Proxy
you can install from npm: npm install --save super-model-js
API
SuperModel( data = {}, emitter = SuperModel.emitter(), store = new Map() )
- create a new model.on(type, func)
,.once(type, func)
- model event listener.on[type](func)
,.once[type](func)
- event listener, type as function property.emit(type, ...values)
- emit an event with values.emit[type](...values)
- emit, event type as a function property.emitAsync(type, ...values)
- emit an event with values (non-blocking).del(key)
- delete model propertymodel(key, =val)
- shorthand function based property get/setmodel({...props})
- update properties with an objectmodel[prop]
- get/set model propertymodel.async[prop]
- get/set model property using promisesmodel.valid(key, =validator)
- make a property validate-able by adding a validator func/RegExpmodel.valid[prop]
- set - prop validator func/RegExp, get - run validator and get bool result.sync(obj, key, modelProperty = key)
- set and update a model property on an object.sync.stop(obj, key)
- stop syncing a model property on an object.store()
- Map containing all model properties.toJSON()
- Get all model.store properties as JSON.toJSONArray()
- Get all model.store properties as "[[key, val], ...]".has(key)
- checks whether model.store has a certain key.emitter()
- just the event emitting part of the model
Learn By Example
Async/Await Values
// view.js const model = feed // news.js modelnews = await await // psst... it also works if you simply assign a promise as is modelnews = await
Data Binding
type something...
const input = documentconst output = document const model = modelsyncmodelsync
Validate Properties
const model = modelvalidusername = /^[a-zA-Z0-9._]{3,55}$/g modelusername = '**Bad Username**' modelvalidusername // -> false model // -> false modelui = ...someUIcomponent modelvalid !uihidden && uiuserlist // or model modelvalidui // -> bool // listen for validation event model // or modelon
Emit Events
more or less node.js style events
const model = modelon modelemit // or model
stop event listeners
const model = const off = modelon // no longer happening
Mutations
internally all values are stored in a map
const model = modelstore // -> Map{"key":"value"} // set modelkey = 'value' // get // -> val modelkey // -> val modelasynckey // -> promise.then(val => {...}) // delete model delete modelkey
listening to mutation events
there are 3 mutation types set, get, del
model.on('type:key', val => {})
model.on('type', (key, val) => {})
const model = model // or modelon modelexistentialism = 'What ever matters to you.'
other things
sync model values with other objects
.sync(obj, key, modelProperty = key)
,
.sync.stop(obj, key)
const model = const PHODelement = document // this keeps obj[key] updated with model[prop] model if nolongerToday // this will update the element's text modelPhilosophyOfTheDay = 'nominalism' else if fedUpWithPositivism modelPhilosophyOfTheDay = 'critical-realism' // stop syncing modelsync
.emitAsync
is the same as .emit
it's just less slightly blocking by running the event loop
and each event listener in a setTimeout(ln, 0)
const model = model