domodel is front-end library that organizes the user interface into models (look) and bindings (behavior) it follows the principle of separation of concerns, it also introduce elements of the observable pattern for the communication between the different parts of the user interface.
npx create-domodel-app [name]
npm install domodel
A model is a JSON representation of a DOM Element.
Let's take this model for example:
export default {
tagName: "button"
}
That would the equivalent of:
const button = document.createElement("button")
A model with children:
export default {
tagName: "div",
children: [
{
tagName: "h2",
identifier: "headline",
textContent: "Unveil a new world"
}
]
}
Notice the textContent
property. You can set any Element properties in this fashion.
The identifier
property is a model property.
Note: The term model will later be used to refer to both the model and its Binding to make it simpler.
You can also ommit the tagName
and you will get a fragment document model:
export default {
children: [
{
tagName: "h2",
identifier: "headline",
textContent: "Unveil a new world"
}
]
}
That would the equivalent of:
const documentFragment = document.createDocumentFragment()
const h2 = document.createElement("h2")
// ...
documentFragment.appendChild(h2)
Most properties listed in your model are defined at the Element level.
However custom properties are not set on the Element as they have unusual behaviors they are treated differently:
tagName
- String - Passed tocreateElement
children
- Array - To add children to an Elementidentifier
- String - To save and retrieve a Nodemodel
- Model - Specify the model that should be ranbinding
- Binding - Specify the Binding to use when running the model (model
property must be set)properties
- Object - Specify the arguments to pass along the Binding (binding
property must be set)
To add a model to the DOM we use the Core.run method provided by the DOModel module and tell it how to add them.
Create a main.js
in src/
, it is the entry point module that is defined in your index.html
:
src/main.js
import { Core } from "domodel" // first we're importing DOModel
// It is preferred to use camel case and suffix model names with "Model" and binding names with "Binding" such as: RectangleModel and RectangleBinding.
import Model from "./model/model.js" // the model we defined earlier, it is our super model
import ModelBinding from ".model/model.binding.js" // the binding we will be defining .bindinglater
window.addEventListener("load", function() { // we only add the
Core.run(Model, {
method: Core.METHOD.APPEND_CHILD, // This is the default method and will append the children to the given parentNode.
binding: new ModelBinding({ myProp: "hello :)" }), // we're creating an instance of our binding (which extends the Binding class provided by DOModel) and passing it to the run method.
parentNode: document.body // the node we want to target in this case it is the node where we want to append the child node using appendChild.
})
})
The documentation for Core.run is available here.
The associated Binding:
src/model/model.binding.js
import { Core } from "domodel" // you could import the library again and run yet another model inside this model
class ModelBinding extends Binding {
onCreated() {
const { myProp } = this.properties
console.log(myProp) // prints hello
// access your model root element through the root property: this.root
// access identifier with the identifier property:
this.identifier.headline.textContent = "The new world was effectively unveiled before my very eyes"
// you might even run another model inside this model
}
}
export default ModelBinding
-
APPEND_CHILD
Append your model toparentNode
-
INSERT_BEFORE
Insert your model beforeparentNode
-
REPLACE_NODE
ReplaceparentNode
with your model -
WRAP_NODE
WrapparentNode
inside your model -
PREPEND
Insert your model before the first child ofparentNode
They are available through Core.METHOD
.
Binding are the behavior part of our model it is required for any model to have a Binding with it.
The following are hooks that your binding can implement to add specific behaviors to your models.
This method will be called before your model is added to the DOM.
This method will be called immediately after your model is added to the DOM.
The following properties are made available from within the the instance of a Binding:
properties
Properties passed along when instancing a binding.root
Root Element of your model.identifier
Hosts individual Element previously tagged in the definition of the model (see Model properties).
Listen to a given observable event. See Binding.listen.
This is the preferred method for listening to events as any listener will be cleaned up when your binding is removed using Binding.remove.
Synonym of Core.run, with the differences being :
parentNode
property is set to the current binding root element.- All
properties
within Binding.properties are inherited by the child model. - A hierarchy of models is created using Binding._children. Making it easier to remove them using Binding.remove.
Remove the model from the DOM. See Binding.remove.
An Observable is a way for your models to communicate with each other.
src/object/observable-example.js
import { Observable } from "domodel"
class ExampleObservable extends Observable {
// you can have a constructor
// getter setter...
// or even better, you could have methods.
}
export default ExampleObservable
Here we associate the EventListener with our current binding and give it properties.observable
as the observable to register the events to.
src/model/model.binding.js
import { Observable, Binding } from "domodel"
import ModelEventListener from "/model/model.event.js"
class ModelBinding extends Binding {
constructor(properties) {
super(properties, new ModelEventListener(properties.observable))
}
}
export default ModelBinding
Any method inside an EventListener
is automatically registered as a listener to the given observable.
src/model/model.event.js
import { EventListener } from "domodel"
class ModelEventListener extends EventListener {
message(data) {
console.log(data)
}
}
export default ModelEventListener
Useful when you want to listen to other parts your UI.
src/model/model.binding.js
import { Observable, Binding } from "domodel"
class ModelBinding extends Binding {
onCreated() {
const observable = new Observable()
observable.listen("message", data => {
console.log(data)
})
}
}
export default ModelBinding
src/model/model.binding.js
import { Observable } from "domodel"
class ModelBinding extends Binding {
onCreated() {
const observable = new Observable()
observable.emit("message", { /* data go here */ })
}
}
export default ModelBinding
Running your model:
import { Core, Observable } from "domodel"
import Model from "/model/model.js"
import ModelBinding from "/model/model.binding.js"
const observable = new Observable()
Core.run(Model, { parentNode: document.body, binding: new ModelBinding({ observable }) })
src/model/application.js
import Model from "./model.js"
export default {
tagName: "div",
children: [
Model
]
}
src/model/application.binding.js
import { Core } from "domodel"
import Model from "./model.js"
import ModelBinding from "./model.binding.js"
class extends Binding {
onCreated() {
Core.run(Model, { parentNode: this.root, binding: new ModelBinding() })
}
}
export default class
src/model/application.js
import Model from "./model.js"
import ModelBinding from "./model.binding.js"
export default {
tagName: "div",
children: [
{
model: Model,
binding: ModelBinding // optionnal
properties: {} // optionnal
identifier: "model" // optionnal
// Any other property is not handled.
}
]
}
In some cases, you might want to reference to a nested model.
You can use the identifier
, it will reference to an instance of the Binding you specified, in this case it would be an instance of ModelBinding
.
Accessing the reference:
src/model/model.binding.js
import { Binding } from "domodel" // you could import the library again and run yet another model inside this model
class extends Binding {
onCreated() {
console.log(this.identifier.model) // returns an instance of ModelBinding
// You could access the root element of the nested model through:
console.log(this.identifier.model.root)
// and much more...
}
}
export default class
See http://domodel.unificator.me.
See https://github.com/topics/domodel-extension.
See https://github.com/topics/domodel-demo.
npm install
npm test