繁体   English   中英

为什么简化的CommonJS Wrapper语法无法在我的Dojo AMD模块上运行?

[英]Why is the Simplified CommonJS Wrapper syntax not working on my Dojo AMD module?

我开始尝试围绕requirejs和新的Dojo AMD结构,但我有一些早期测试的问题:

CG / signup.js:

define(['dojo/_base/fx', 'dojo/dom'], function(fx, dom){    
    return function(){
        this.hidePreloader = function(id){
            var preloader = dom.byId(id);
            fx.fadeOut({node : preloader}).play()
        }
    }
})

这很好用。 在master cg.js文件中:

require(['dojo/_base/kernel', 'dojo/_base/loader'])
dojo.registerModulePath('cg', '../cg')

require(['cg/signup', 'dojo/domReady!'], function(Signup){
        var sp = new Signup();
        sp.hidePreloader('preloader')
})

巴姆。 完成。 但是,在使用Simplified CommonJS Wrapper结构时:

define(function(require){    
    var fx = require('dojo/_base/fx'),
        dom = require('dojo/dom');

    return function(){
        this.hidePreloader = function(id){
            var preloader = dom.byId(id);
            fx.fadeOut({node : preloader}).play()
        }
    }
})

我收到一个undefinedModule错误。 它似乎来自dojo/_base/fx系列,但我不知道为什么。

UPDATE

为了澄清。

index.html脚本

<script type="text/javascript" src="js/dojo/dojo.js.uncompressed.js" data-dojo-config="isDebug:true,async:true"></script>
<script type="text/javascript" src="js/cg.js"></script>

cg.js

require(['dojo/_base/kernel', 'dojo/_base/loader'])
dojo.registerModulePath('cg', '../cg')

require(['cg/signup', 'dojo/domReady!'], function(signup){
    signup.testFunc()
})

JS / CG / signup.js

define(['require', 'exports'], function(require, exports){
    var dom = require('dojo/_base/kernel');
// Any other require() declarations (with very very few exceptions like 'dojo/_base/array throw undefinedModule errors!!!

    // without any error causing requires, this works fine.
    exports.testFunc = function(){
        alert("hello")
    }
})

dojo完全支持Simplified CommonJS Wrapper格式。 但是,有一个先决条件......你必须没有依赖数组。

define(function (require, exports, module) {
    var fx = require('dojo/_base/fx'),
        dom = require('dojo/dom');

    // continue...
});

这不会起作用

define(['require', 'exports', 'module'], function (require, exports, module) {
    var fx = require('dojo/_base/fx'),
        dom = require('dojo/dom');

    // continue...
});

这也不会......

// in this case require, exports and module will not even exist
define([], function (require, exports, module) {
        var fx = require('dojo/_base/fx'),
        dom = require('dojo/dom');

    // continue...
});

请注意,在执行构建时,Dojo不支持简化的CommonJS样式以进行依赖项检测。 这意味着您需要使用常规依赖项列表样式,或者在构建配置文件中定义层时必须复制所有依赖项。

这是他们跟踪器中的相关错误:

http://bugs.dojotoolkit.org/ticket/15350

这是一个围绕Dojo定义的小包装,它使用从RequireJS获取的代码来根据定义函数的toString计算依赖关系。 它包装全局命名空间中的当前“define”,计算依赖关系,然后调用包装的define。

defineWrapper.js:

// Workaround for the fact that Dojo AMD does not support the Simplified CommonJS Wrapper module definition
(function() {
    var commentRegExp = /(\/\*([\s\S]*?)\*\/|([^:]|^)\/\/(.*)$)/mg,
      cjsRequireRegExp = /require\(\s*["']([^'"\s]+)["']\s*\)/g,
      ostring = Object.prototype.toString;
    function isArray(it) {
      return ostring.call(it) === '[object Array]';
    }
    function isFunction(it) {
      return ostring.call(it) === '[object Function]';
    }
    var oldDefine = define;

    define = function(name, deps, callback) {
        //Allow for anonymous functions
        if (typeof name !== 'string') {
          //Adjust args appropriately
          callback = deps;
          deps = name;
          name = null;
        }

        //This module may not have dependencies
        if (!isArray(deps)) {
          callback = deps;
          deps = [];
        }

        //If no name, and callback is a function, then figure out if it a
        //CommonJS thing with dependencies.
        if (!deps.length && isFunction(callback)) {
          //Remove comments from the callback string,
          //look for require calls, and pull them into the dependencies,
          //but only if there are function args.
          if (callback.length) {
              callback
                  .toString()
                  .replace(commentRegExp, '')
                  .replace(cjsRequireRegExp, function(match, dep) {
                      deps.push(dep);
                  });

              //May be a CommonJS thing even without require calls, but still
              //could use exports, and module. Avoid doing exports and module
              //work though if it just needs require.
              //REQUIRES the function to expect the CommonJS variables in the
              //order listed below.
              deps = (callback.length === 1 ? ['require'] :
                  ['require', 'exports', 'module']).concat(deps);
          }
        }

        if(name === null) {
            return oldDefine(deps, callback);
        } else {
            return oldDefine(name, deps, callback);
        }
    }
})();

你会怎么用?

<script src="...dojo..."></script>
<script src="defineWrapper.js"></script>
<script>require(["some_simplified_commonjs_defined_module"], function(module) {
   // use module here
});</script>

require定义吗? 我不知道那个参数值会来自何处。 我想你可能需要做类似的事情

define(["require"], function(require){ ...

我开始认为也许我不打算学习Dojo。 但是,这一切都伴随着更多的阅读。 我不确定我做了什么不同或其他什么,但这是工作布局。

index.html脚本和配置

<script type="text/javascript">
dojoConfig = {
    async : true,
    isDebug : true,
    debugAtAllCosts : true,
    packages : [{
        name : 'cg',
        location : '/../js/cg'
    }]
}
</script>
<script src="http://ajax.googleapis.com/ajax/libs/dojo/1.7.1/dojo/dojo.js"></script>
<script type="text/javascript" src="js/cg.js"></script>

JS / cg.js

require(['cg/signup', 'dojo/ready'], function(signup){
    signup.init('preloader')
})

JS / CG / signup.js

define(['dojo', 'require'], function(dojo, require){
    var fx = require('dojo/_base/fx')

    return new function(){    
        this.init = function(id){
            fx.fadeOut({node : dojo.byId(id)}).play()
        }
    }
})

同样,不完全确定为什么var fx = require(...)语句在这个语句中的工作方式与其他语句的工作方式不同,可能是我下载的构建版本与关心的CDN。 有用。 我曾经在同一条船上帮助其他人的一些链接:

编写模块化JS

AMD与CommonJS Wrapper

Dojo Toolkit AMD

Dojo Config(1.7)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM