0

dijit と独自のウィジェット (_WidgetBase ベース) の機能を拡張する一連の mixin の作成を開始しました。1.8 宣言型を使用して、data-dojo-mixins を使用すると、パーサーはそれらで必要なことを行います。

ただし、いくつかの場所では、ウィジェットをプログラムでインスタンス化しています。この/これらの他のクラスが混在するこのクラスをインスタンス化するよう Dojo に指示する方法はありますか? それとも、safeMixin を個別に使用する必要がありますか?

どんなアドバイスも役に立ちます、ありがとう。

4

2 に答える 2

1

拡張するカスタムウィジェットを常に作成する方法は次の_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));
});
于 2013-10-10T19:31:07.137 に答える