Dojo 1.10 Widget不是构造函数

时间:2017-11-01 04:02:40

标签: javascript dojo widget

我想要在名为Javascript的另一个Core.js文件中实例化以下自定义窗口小部件。

信息中心小组件

define([
"dojo/_base/declare",
"dojo/_base/config",
"dojo/ready", 
"dojo/_base/window",
"dijit/_WidgetBase",
"dijit/_TemplatedMixin",
], function(
   v_declare,
   v_config,
   v_ready,
   v_window,
   v_WidgetBase,
   v_TemplatedMixin,
) {
  return v_declare("dashboard", [v_WidgetBase, v_TemplatedMixin], {
    templateString: "<div>hello world</div>",
    constructor: function(setting) {
        console.log("smtg..");
    },
    buildRendering: function() {
        var v_context = this;
        var v_domNode = this.domNode;
        this.inherited(arguments);

        console.log('Expeerimental');
    },
    init: function() {
        console.log('Expeerimental');
    }
});
});

在Core.js上实现Dashboard Widget

require([
"dojo/_base/config",
"dojo/_base/declare",
"dojo/dom",
"dojo/dom-class",
"dojo/dom-style",
"dojo/on",
"dojo/topic",
"dojo/domReady!",
"ui/dashboard/dashboard"
], function(
    b_config,
    b_declare,
    b_dom,
    b_domClass,
    b_domStyle,
    b_on,
    b_topic,
    p_dashboard
 ) {

start();


function start(){
    console.log(p_dashboard);
    var dashboard = new p_dashboard();
};

});

但是,在var dashboard = new p_dashboard();行,我收到以下错误:

  

TypeError:p_dashboard不是构造函数       在HTMLDocument._461   (dojo.js:8)“在domReady回调中”“TypeError:p_dashboard不是构造函数

我似乎无法弄清楚为什么会出现这个错误。有任何想法请赐教?谢谢

2 个答案:

答案 0 :(得分:0)

我认为你的错误在于模块的加载顺序,

在你的Core.js ui/dashboard/dashboard窗口小部件在Ready插件之后加载,在回调函数中,它在topic模块之后直接引用(知道dojo / domReady!isn'在calback中引用的be beacause常见约定不是在回调函数中指定返回变量)

因此,尝试在ui/dashboard/dashboard之前创建dojo/domReady!,如果在dojo配置中配置好路径,dojo将正确实例化它。

它如下所示:

require([
    "dojo/_base/config",
    "dojo/_base/declare",
    "dojo/dom",
    "dojo/dom-class",
    "dojo/dom-style",
    "dojo/on",
    "dojo/topic",

    "ui/dashboard/dashboard",

    "dojo/domReady!"
], ..... 
);

答案 1 :(得分:0)

使用9个元素的数组调用require并仅在函数中使用8.最后(第8个)元素为dojo/domReady!

require([
    "dojo/_base/config",     // 1 
    "dojo/_base/declare",    // 2
    "dojo/dom",              // 3
    "dojo/dom-class",        // 4
    "dojo/dom-style",        // 5
    "dojo/on",               // 6
    "dojo/topic",            // 7
    "dojo/domReady!",        // 8
    "ui/dashboard/dashboard" // 9
], function(
    b_config,                // 1. dojo/_base/config
    b_declare,               // 2. dojo/_base/declare
    b_dom,                   // 3. dojo/dom
    b_domClass,              // 4. dojo/dom-class
    b_domStyle,              // 5. dojo/dom-style
    b_on,                    // 6. dojo/on
    b_topic,                 // 7. dojo/topic
    /*********************************************************
                 Where is my dojo/domReady!??????
    *********************************************************/
    p_dashboard              // 8. ui/dashboard/dashboard
) {
    // ...
});

所有,您必须做的是在require的数组末尾移动“dojo / domReady”:

require([
    "dojo/_base/config",
    "dojo/_base/declare",
    "dojo/dom",
    "dojo/dom-class",
    "dojo/dom-style",
    "dojo/on",
    "dojo/topic",
    "ui/dashboard/dashboard",
    "dojo/domReady!"
], function(
    b_config,   // dojo/_base/config
    b_declare,  // dojo/_base/declare
    b_dom,      // dojo/dom
    b_domClass, // dojo/dom-class
    b_domStyle, // dojo/dom-style
    b_on,       // dojo/on
    b_topic,    // dojo/topic
    p_dashboard // ui/dashboard/dashboard
                // dojo/domReady! is the self-called function
) {
    // ...
});