Dojo 1.7如何使用dojo组件外面的require() [英] Dojo 1.7 how to use dojo components outside of require()

查看:123
本文介绍了Dojo 1.7如何使用dojo组件外面的require()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var myCpane; 

require([
dijit / layout / ContentPane
],function(ContentPane){
myCpane = new ContentPane();
}) ;

myCpane.startup(); //它给出'myCpane'为未定义

在上面的例子中,在最后一个例子中,变量'myCpane'来到'undefined',如果我在'require()'回调函数内使用'myCpane.startup()',那么它将会正常工作。



但是,我想在'require'函数外部使用'myCpane'变量(很多原因)。我知道由于Dojo的组件加载过程而导致的'require()'回调函数执行延迟。



我的问题是,


  1. 如何阻止'require()'函数,直到执行它的回调函数为止。

所以当控件从'require()'函数出来时,变量'myCpane'不会是'undefined'



====== ================================================== ===



为了克服这个问题,我已经写了一个小功能加载模块,等待模块加载完成

  LoadModule:function(modulePath){// modulePath =dijit / layout / ContentPane
var moduleObject = undefined;

require({async:false},[modulePath],function(getModuleObject){
moduleObject = getModuleObject;
});

//等待模块加载完成
while(moduleObject === undefined);

//返回加载的模块。
return moduleObject;
}

函数的输出总是执行while循环,控件永远不会在'require()'的回调函数内部将值设置为变量moduleObject。



当'require()'函数调用它的回调函数时?我已经使用浏览器调试器窗口验证文件ContentPane.js是否正确加载,但是没有调用回调函数,如果我注释了while循环,则回调被正确调用。



当我的情况下控件将进入回调函数内部

解决方案

我不确定你想要实现什么,但它会像我的编程反模式。无论如何,您可以通过 dojo / _base / Deferred 实现此目的:

  require ([dojo / _base / Deferred],function(Deferred){

var deferred = new Deferred();

require([dijit / layout / ContentPane ],function(ContentPane){
var myCpane = new ContentPane();
deferred.resolve(myCpane); //解析,即调用`then`回调
});

deferred.then(function(myCpane){
console.log(myCpane);
myCpane.startup();
});

});

在jsFiddle中调用它: http://jsfiddle.net/phusick/HYQEd/



我也建议你考虑这两个策略之一实现相同:


  1. ContentPane code> id 并通过dijit的 registry.byId()获取其引用。

  2. 创建 Co ntentPane 实例在一个单独的模块中,并将其显示为该模块的返回值:

      //文件:myCpane.js 
    define([dijit / layout / ContentPane],function(ContentPane){
    var myCpane = new ContentPane();
    return myCpane;
    });


    // file:main.js
    require([./ myCpane],function(myCpane){
    myCpane.startup();
    });



I have created Dojo widget like below using AMD loader in Dojo 1.7.2

var myCpane;

require([
            "dijit/layout/ContentPane"
        ], function(ContentPane) {
        myCpane = new ContentPane();
});

myCpane.startup();  // It gives 'myCpane' as undefined

In the above example, in the last statment, the variable 'myCpane' is coming as 'undefined', if I use the 'myCpane.startup()' inside the 'require()' callback function then, it will work fine.

But I want to use that 'myCpane' variable on outside of the 'require' function (for many reasons). I know the 'require()' callback function execution delayed due to the component loading process by Dojo.

My question is,

  1. How to block the 'require()' function until it completes to execute it's callback function.

So the variable 'myCpane' will not be 'undefined' when the control come out from the 'require()' function

===========================================================

To overcome this issue, I have written a small function to load the modules and wait until the module load complete

LoadModule: function(modulePath) { // modulePath = "dijit/layout/ContentPane" 
        var moduleObject = undefined; 

        require({async:  false}, [modulePath], function(getModuleObject) { 
                moduleObject = getModuleObject; 
        }); 

        // Wait until the module loads completes 
        while(moduleObject === undefined); 

        // Return the loaded module. 
        return moduleObject; 
} 

The output of the function is always executing the while loop, the control never comes inside of 'require()'s callback function to set the value to the variable "moduleObject".

When the 'require()' function will call it's callback function? I have verified using the browser debugger window the file 'ContentPane.js' is loaded properly, but the callback function is not called, If I comment the while loop then, the callback is called properly.

When the control will come inside of the callback function in my case ?

解决方案

I'm not sure what are you about to achieve, but it looks for me like a programming anti-pattern. Anyway you can achieve this via dojo/_base/Deferred:

require(["dojo/_base/Deferred"], function(Deferred) {

    var deferred = new Deferred();

    require(["dijit/layout/ContentPane"], function(ContentPane) {
        var myCpane = new ContentPane();
        deferred.resolve(myCpane); //resolve, i.e. call `then` callback
    });

    deferred.then(function(myCpane) {
        console.log(myCpane);
        myCpane.startup();
    });

});​    

Mess with it at jsFiddle: http://jsfiddle.net/phusick/HYQEd/

I would also suggest you consider one of these two strategies to achieve the same:

  1. Give the ContentPane an id and obtain its reference via dijit's registry.byId().
  2. Create ContentPane instance in a separate module and expose it as a return value of that module:

    // file: myCpane.js
    define(["dijit/layout/ContentPane"], function(ContentPane) { 
        var myCpane = new ContentPane();
        return myCpane;
    });
    
    
    // file: main.js
    require(["./myCpane"], function(myCpane) {
        myCpane.startup();
    });
    

这篇关于Dojo 1.7如何使用dojo组件外面的require()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆