config
The config
plugin doesn't expose useful methods. But it serves a useful purpose for spirits that like to be configured from the server side or in a template file.
Config properties
When the spirit initializes, the plugin will collect all DOM attributes prefixed with gui
.
<div class="my-spirit" gui.visible="false"></div>
This happens exactly when the onconfigure
method is called. The name and value of gui
attributes will be mapped directly onto JavaScript properties of the spirit.
gui.channel('.my-spirit', gui.Spirit.extend ({
visible: true, // must be declared
onconfigure: function() {
this.super.onconfigure();
if(!this.visible) {
this.dom.hide();
}
}
}));
The spirit must declare the property in advance, otherwise an error will be thrown, just to make sure that we remember to update the HTML whenever we rename the property.
The attribute value will be converted to an appropriate type, in this case a boolean
. In the example below, the attribute values will be converted to a number
, an array
and an object
.
<div class="my-spirit"
gui.maxcount="23"
gui.counters="[1,2,3]"
gui.mincount="{value: 23}">
</div>
Actually support arrays and objects.
The gui
attribute can also configure nested objects. Here we configure an imaginary randomizer
plugin that generates random numbers. You can check out how to create plugins if you like.
<div class="my-spirit"
gui.randomizer.maxvalue="23"
gui.randomizer.minvalue="5">
</div>
Config methods
If the attribute name matches a method, the method will be invoked with with the attribute value as an argument. Again, the value will be converted to a convenient type. In this case, it gets to stay a string.
<div class="my-spirit" gui.logmessage="Hello world"></div>
gui.channel('.my-spirit', gui.Spirit.extend ({
logmessage: function(message) {
console.log(message);
}
}));
Importantly, a method gets triggered at onready
while a property gets set a onconfigure
. Please refer to the spirit lifecycle for an overview of these events. You can say that:
- Properties configure how a spirit should start up
- Methods configure what it should do when it's ready
You can implement the property with an accessor if you need to run some code when the plugin sets the property (during onconfigure
).
Make sure that link leads to something