Sprincul class is the entry point for the framework. All methods are static — you never instantiate it directly. You use it to register your model classes, run initialization, listen for the ready event, and interact with the global key/value store. Everything must be registered before you call Sprincul.init().
Registration
Sprincul.register(name, modelClass)
Registers a single model class under a name. The name you provide here must match the value you set in data-model attributes in your HTML. Call this before Sprincul.init().
The identifier for this model. Must match the
data-model attribute value in your HTML.A class that extends
SprinculModel. Sprincul calls new modelClass(element) for each matching element it finds.Sprincul.registerAll(models)
Registers multiple model classes at once from a plain object. Each key becomes the data-model name for that class. Use this when you have several models to register — it keeps your entry point tidy.
An object whose keys are model names and whose values are classes that extend
SprinculModel.The object keys are used verbatim as the model names. If you name the key
Counter, your HTML must use data-model="Counter" — casing matters.Initialization
Sprincul.init(options?)
Scans the DOM for all elements with a [data-model] attribute, creates a model instance for each one, wires up bindings and event listeners, and runs the lifecycle hooks (beforeInit, then afterInit) on each instance. Page-level data-cloaked elements (those not on a data-model element itself) are uncloaked immediately after all afterInit hooks are called.
Calling init() more than once is safe — elements that have already been initialized are skipped. This means you can inject new markup and call init() again to hydrate only the new roots.
Optional configuration object.
When
true, Sprincul logs console warnings for misconfigured bindings and handlers, and includes the live model instance on each SprinculModelInfo object in the ready callback and sprincul:ready event. Disable in production — exposing instances allows console access to internal state.Sprincul.onReady(callback)
Registers a function to be called once all models’ afterInit hooks have been invoked. Use this to run logic that depends on the entire page being wired up — for example, reading initial model state or connecting cross-model integrations.
onReady callbacks are one-shot per init cycle. After they fire, the internal list is cleared. If you call Sprincul.init() again (for example, after injecting new markup), you must re-register any callbacks you want to run in the new cycle.
A function that receives an array of
SprinculModelInfo objects — one for each model root that was initialized in this cycle.devMode, each item in the array also contains instance:
sprincul:ready DOM event directly if you prefer not to use the helper:
Global store
Sprincul.store is a lightweight, reactive key/value store shared across all model instances. You can seed it before init(), read it from inside models, and subscribe to changes anywhere on the page. It uses nanostores atoms under the hood, so updates are efficient and subscription callbacks fire synchronously after each change.
Sprincul.store.get<T>(key)
Returns the current value for a key, or undefined if no value has been set.
The store key to read.
Sprincul.store.set<T>(key, value)
Sets a value for a key. Creates the entry if it does not exist yet. All active subscribers for this key are notified immediately.
The store key to write.
The value to store. Can be any serializable type.
Sprincul.store.subscribe<T>(key, callback)
Subscribes to changes for a key. The callback fires after a value is set — it does not fire with the current value at subscription time. To read the current value immediately, call store.get() first or seed a default with store.set().
Returns an unsubscribe function. Call it to stop receiving updates.
The store key to watch.
Called with the new value whenever the key is updated. Receives
undefined if the key is cleared.Sprincul.store.clear()
Removes all keys and their subscribers from the store. Useful in tests or when tearing down a page section entirely.
Exported types
These types are exported from thesprincul package and available for use in TypeScript projects.
SprinculModelInfo
Describes a single model instance returned by onReady callbacks and the sprincul:ready event.
The model name as registered with
Sprincul.register() or Sprincul.registerAll().The DOM element marked with
data-model.The live model instance. Only present when
devMode: true is passed to Sprincul.init().SprinculModelConstructor
The constructor signature that Sprincul.register() and Sprincul.registerAll() expect. Any class that extends SprinculModel satisfies this type automatically.