Properties:
Name | Type | Description |
---|---|---|
model |
Object | the model passed to the constructor. |
Bi-directional model ⟷ DOM bindings.
Methods
(inner) new(model)
Constructor.
Example
var nexus = dom.Nexus.new(model);
Parameters:
Name | Type | Description |
---|---|---|
model |
Object | a plain object storing your model. |
(inner) bind(component, name, eventNameopt, view)
Bind a property in model
to a component, on the given event name.
A component is an object with three fields:
- new: function ComponentName(property, nexus, ...)
→
Element
Build a new HTML element with any structure you need, and bind it to the givenproperty
in the model innexus
.
property
is the property/field name in the model, and
nexus
is a Nexus object built withnew dom.Nexus(model)
other parameters depend on the component. You can define this function with any parameters in any order, but normally you will need at leastproperty
andnexus
to bind it to the model.
Useful for creating the whole view in Javascript. - use: function ComponentName(view, property, nexus, ...)
→
Element
Take the given HTML and bind it to the givenproperty
in the model innexus
.
Useful for activating views built in HTML. - get: function get(element) →
string
- set: function set(element, value)
Example
<!DOCTYPE "html">
<html>
<head>
<meta charset="utf-8"/>
<style type="text/css">
textarea { float:left; margin-right:1em; width:30%; }
</style>
</head>
<body>
<script src="dom.js"></script>
<script type="application/javascript">
var model = { html: '<h1>Hello!</h1>\n<p>How are you?</p>\n' };
var nexus = dom.Nexus.new(model);// nexus.model === model
var TextArea = {
new: function TextArea(property, nexus, attributes) {
var view = dom(['textarea', attributes]);
view.setAttribute('name', property);
return this.use(view, nexus);
},
use: function TextArea(view, nexus) {
return nexus.bind(this, view.name, 'input', view);
},
get: function get(view) { return view.value; },
set: function set(view, value) { view.value = value; }
};
// This adds the component to the document's body element:
dom([document.body, TextArea.new('html', nexus, {
'autofocus': true,
'placeholder': 'Some text',
'rows': 40
})]);
var htmlPreview = dom(['div.preview']);
dom([document.body, htmlPreview]);
// Watch changes in model.currentText and parse it as HTML:
nexus.addListener('html', function (model, name) {
htmlPreview.innerHTML = model[name];
});
</script>
</body>
</html>
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
component |
Component | object with new, use, get, set members. |
|
name |
string | property name in the model. |
|
eventName |
string |
<optional> |
event fired when the component changes,
can be |
view |
Element | HTML element that is the view of the component. |
(inner) changed(name, originopt)
Notify this Nexus of external changes in the model.
This can be nested: one Nexus for the main model, others for sub-models inside.
Example
var model = { someProperty: "someValue", submodel: { "a": 1 } };
var parentNexus = dom.Nexus.new(model);
var childNexus = dom.Nexus.new(model.submodel);
// Notify parent when properties change here:
childNexus.addListener("*", function (model, property, origin) {
parentNexus.changed('submodel', origin);
});
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
name |
string | field name in the model,
or '*' to indicate changes in unspecified fields
so that all listeners will be notified.
Several names* can be given at once as an array of strings:
in that case, the listeners
(dom.Nexus.addListener(name, listener))
will receive the first name their |
|
origin |
Element |
<optional> |
view that caused the change, or null. You might use this to avoid infinite loops: the origin element will not recieve this event. |
(inner) addListener(name, listener) → {Object}
Add a listener for changes in a field in the model.
The listener will be called immediately as a way of triggering
initialization. In this first call, the origin
parameter
will be a literal false
to distinguish it from later changes.
Parameters:
Name | Type | Description |
---|---|---|
name |
string | field name in the model, or '*' to listen for any changes. |
listener |
Listener |
|
Returns:
- reference that can be used later to remove this listener with dom.Nexus.removeListener(listenerReference).
- Type
- Object