拡張するカスタムウィジェットを常に作成する方法は次の_WidgetBase
とおりです。
define([
'dijit/_WidgetBase',
'path/to/module1',
'path/to/module2' /* etc. */
], function(WidgetBase, module1, module2) {
/* Here you can define your module's functions */
var myVar = 42;
var foo = function(num){
alert("My var is " + myVar);
};
return declare([WidgetBase], {
/* This is the widget you are creating. Public variables should go here */
myGlobalVar = "I did stuff",
doStuff: function(a, b) {
module1.doSomething(a);
module2.doSomethingElse(b);
alert(this.myGlobalVar);
},
callDoStuff: function() {
alert("I'm gonna doStuff");
this.doStuff(3, 5);
}
});
});
もちろん、プログラムでウィジェットを拡張したいだけなら、dojo._base.lang::extend()
(ウィジェットを文字通り拡張するために) を使用するか、dojo._base.lang::mixin()
(ウィジェットのプロトタイプを変更するために) いつでもフォールバックできます。
DojoToolkit Web サイトから:
require(["dojo/_base/lang", "dojo/json"], function(lang, json){
// define a class
var myClass = function(){
this.defaultProp = "default value";
};
myClass.prototype = {};
console.log("the class (unmodified):", json.stringify(myClass.prototype));
// extend the class
lang.extend(myClass, {"extendedProp": "extendedValue"});
console.log("the class (modified with lang.extend):", json.stringify(myClass.prototype));
var t = new myClass();
// add new properties to the instance of our class
lang.mixin(t, {"myProp": "myValue"});
console.log("the instance (modified with lang.mixin):", json.stringify(t));
});