Observe-Literal
An Observable implementation for javascript literals. The API style is leaned on the jQuery Event API.
Installation:
npm install observe-literal
Usage
- module import
- html import
- create:
const obs = ""const obs = const obs = {}const obs = 0
What you get:
About the Observable: ...
{ // fields initialValue: any, // the initial value you passed in the constructor. isInInitialState: boolean, // whether the value has been changed or not. // methods update: function, // a method to override the value. you can use the setter aswell o.value = "new-value" add: function, // a method to add information to the value. internally decides about the action depending on the type of the initial value. remove: function, // add in reverse. on: function, // a method to add event listeners. off: function, // a method to remove event listeners. reset: function, // a method to reset the value to its initial state. clearCallbacks: function, // a method to clear all callbacks.}
- add event listener
Available Events: before-update, update, add, remove, reset
// before the observable value gets update you can do something with its old valueobs// if you listen to update you get your callback executed with the newest value // everytime there is an update.obs// you can listen to the reset of the observable, to maybe reset the view.obsobsobs // you can listen on multiple events at onceobs
- remove event listener
// remove a single callback from a single eventobs // remove a single callback from all eventsobs // remove update event listenersobsobs // remove all callbacks at onceobs