Datasheet
Chapter 1: Enhancing Development with Dojo Core
Defining a Class to Support Declaration in Markup
Now, check out this implementation for the
decafbad/thingA.js
module:
dojo.provide("decafbad.things");
dojo.declare("decafbad.things.thingA", null, {
alpha: false,
beta: [ ‘one’, ‘two’ ],
foo: ‘default’,
baz: 456,
constructor: function(args, node) {
dojo.mixin(this, args);
dojo.query(‘span’, node).forEach(function(ele) {
var name = ele.className;
var val = this[name];
ele.innerHTML = val ?
‘[’ + (typeof val) + "] " + val :
‘undefined’;
}, this);
}
});
This isn’t much code, but there’s a lot going on here — this is a theme throughout Dojo.
First, the module itself is established with a call to
dojo.provide()
, then the declaration of the class
decafbad.things.thingA
is begun.
The class starts off with several properties, each a different JavaScript type. This is important to note,
because the parser peeks into the class and performs type conversions from attribute character data as
appropriate based on the original types of the default values. Hopefully, this feature will make more
sense once you’ve seen it in action.
After the property definitions comes the constructor, whose parameters are supplied by the parser:
❑
args
— An object collecting the attributes from markup
❑
node
— A reference to the DOM element declaring the object instance
In the constructor implementation, the first thing is a call to
dojo.mixin(this, args)
.Thisisaquickway
to take all of the incoming converted attributes from
args
and assign them to object properties — like
mixin classes,
dojo.mixin()
mixes the given properties into the given object.
The next part is a
dojo.query()
chained with a
forEach()
call. This sort of construct is explored in
more detail in the next chapter. In short, it searches the contents of
node
for
<span>
elements and applies
16