answer that … but it appears that "out of the box", JavaScript doesn't have the concept of classes,
inheritance or other common OO functions. However, Dojo provides the ability to achieve these
concepts through the Dojo "declare" function that is part of the "dojo/_base/declare"
package.
Let us start with an example. Let us assume we wish to create a new class called "MyClass" that
is in a "package" called "myPackage". The first step is to create a directory tree associated with
the package. So we might create a directory called "myPackage". In that directory, we would
now create a JavaScript source file with the name of the class we wish to define. For example a file
called "MyClass.js".
Within the MyClass.js, we now code our class:
define(["dojo/_base/declare"], function(declare) {
return declare(null, {
constructor: function(a, b, c) {
this.a = a;
this.b = b;
this.c = c;
} // End of constructor
}); // End of declare
}); // End of define
With this defined, we can now use it by:
require(["myPackage/MyClas"], function(MyClass) {
var myObject = new MyClass("a1", "b1", "c1");
});
See also:
• Dojo Reference – declare – 1.9
Creating Custom Widgets
Dojo allows us to create our own custom Widgets. The result of this are new widgets that can be
used just like the Dojo supplied widgets. After a new widget is written and documented, it can be
consumed by a Dojo programmer and the details of its own internal implementation can then be
hidden from the consumer. In order to build custom widgets, we need to start understanding some
of the building blocks that are involved.
First there is the idea of a template. This is an HTML document fragment that will be inserted
into the page to visualize the widget. This is optional as the widget could also use JavaScript to
build its HTML content. Of course, a hybrid approach can also be applied where both HTML
templates and JavaScript are used in conjunction with each other.
Next there are any CSS Styles that need to be applied.
Finally, there is the JavaScript that provides execution semantics.
A new widget will include code:
declare("new widget name", base widget);
eg.
declare("diji.form.TextBox", dijit.form._FormWidget);
Custom widgets will inherit from dijit/_WidgetBase. By doing this, a life cycle is defined.
• constructor
• postscript
Page 202
Komentarze do niniejszej Instrukcji