The data-bind-<prop> attribute
You add a data-bind-<prop> attribute to any element inside a data-model container. The attribute name encodes the state property to watch, and the attribute value is the name of the callback method on your model that Sprincul should call when that property changes.
this.state.count changes, Sprincul calls showCount(element), passing the bound element as the first argument. Your callback reads from this.state and updates the element however it needs to — setting textContent, toggling a class, updating an attribute, anything.
Sprincul also calls each binding callback once immediately when bindings are first set up, so the element reflects the initial state without you having to trigger a change manually.
How the binding pattern works
1
Parse the attribute
During
Sprincul.init(), Sprincul reads every data-bind-* attribute inside the model’s container. The part after data-bind- is the state property name; the attribute value is the callback method name.2
Call the callback immediately
Sprincul calls the callback right away with the bound element, so the element reflects the current (initial) state.
3
Watch for state changes
Whenever
this.state.<prop> is assigned a new value, Sprincul schedules a DOM update via requestAnimationFrame. On the next frame, it calls the callback again with the same element.this.state.
Lowercase state property names
A full example: Profile
The followingProfile model and HTML show how bindings connect two state properties to two separate elements. Sprincul calls showName and showEmail whenever the corresponding state property changes.
Counter bindings
TheCounter model uses the same pattern with a number input. Multiple elements can bind to the same state property — Sprincul calls each callback independently.
Multiple bindings for the same property
You can putdata-bind-<prop> on as many elements as you need. When the state property changes, Sprincul calls the associated callback for each bound element. The callbacks can be different methods, or the same method — whatever makes sense for each element.
Bindings must be inside the model container
Sprincul only processesdata-bind-* attributes that are direct or nested descendants of the data-model container. Elements outside the container are ignored. Nested data-model elements are managed by their own model instance and are not included in the parent’s bindings.