Datasheet
Chapter 1: Enhancing Development with Dojo Core
Extending Existing Classes with dojo.extend()
Using multiple inheritance in declaring new classes with mix-and-match functionality is handy, but what
can be even handier is the ability to augment existing classes. This is where
dojo.extend()
comes in:
dojo.extend(decafbad.school.Person, {
_studying: null,
study: function(subject) {
this._studying = subject;
return "Now studying "+subject;
}
});
This code augments the
Person
base class with a new
study()
method and a new data member to keep
track of what’s being studied. What’s particularly nice is that additions to the base class carry down to
subclasses. Take this creation of a
DormAssistant
object, for example:
var bar = new decafbad.school.DormAssistant(‘kim’, ‘senior’);
bar.study(‘physics’);
Using
dojo.extend()
allows you to layer in custom functionality in a powerful way atop existing wid-
gets and classes. This post-declaration augmentation of existing classes offers a way to package up
alterations and tweaks to your own classes — as well as those belonging to Dojo itself.
The preceding
dojo.extend()
call doesn’t even need to be in the same module as the original
Person
class. You can include
dojo.extend()
statements as part of an external package for use with
dojo.require()
, with no pre-planned coordination with other existing packages and classes.
Declaring Objects in Markup
As you’ve already seen in the first part of this chapter, part of the mojo of Dojo is the declaration of objects
in HTML markup. This is most immediately useful in the context of widgets that wrap DOM elements
with additional functionality. However, this feature is not limited to widgets: Using the Dojo parser, you
can declare the instantiation of any Dojo class through HTML markup.
The
dojo.parser
module provides the Dojo parser. You can enable it by ensuring that
parseOnLoad
is
true
in
djConfig
, and that
dojo.require("dojo.parser")
appears in your page scripts. This is all true
for the sample offered at the start of the chapter.
On page load, the parser scans though the DOM looking for elements bearing a
dojoType
attribute — the
existence of the attribute signifies that the element is an object declaration, and the value of the attribute
specifies the class of the object to instantiate.
Declaring an Object
Diving right in, consider the following HTML code:
<html>
<head>
<title>Hello Dojo Parser</title>
14