Define a model
Each component is a standard JavaScript class that extends
SprinculModel. Initialize your reactive state on this.state inside beforeInit(), then add methods to update the DOM and handle events.The beforeInit() hook runs before Sprincul attaches any bindings, so it is the right place to set up initial state and computed properties. Any method you define on the class can be referenced directly from your HTML.this.state.count is reactive. Whenever its value changes, Sprincul automatically calls any DOM-bound callbacks that depend on it — in this case, showCount.Register and initialize
Before calling When you have multiple models, When
Sprincul.init(), register every model class you want Sprincul to recognize. Use register() for a single model, or registerAll() to register several at once.registerAll() keeps things tidy:Sprincul.init() runs, it scans the DOM for every element with a data-model attribute, creates an instance of the matching class, wires up your data-bind-* bindings and event listeners, and then runs the lifecycle hooks (beforeInit followed by afterInit).Annotate your HTML
Wrap each model in a container element with a Here is how each attribute type works:
data-model attribute set to the name you registered. Place your bindings and event attributes anywhere inside that container.data-model="Counter"— marks this element as the root of a Counter model instance. Sprincul creates one class instance per root element.data-bind-count="showCount"— wheneverthis.state.countchanges, Sprincul callsshowCount(element)with the bound element as the argument, letting your method update it however you like.onclick="decrement"— Sprincul converts this into a properaddEventListener('click', ...)call on your model instance, then removes the inline attribute. The nativeEventobject is passed to the handler.
main.js as a module, open the page, and the counter is live: