是的,但确切的语法与问题中使用的不同。
加载字符数据的插件是dojo/text。
在加载 JavaScript 库时不应使用扩展名,并且文件的位置是通过 dojotoolkit 库的相对位置或 dojoConfig 中的 packages 位置声明设置的:
require(['underscore'], function( _ ){
_.reduce ...
});
在 Dojo 配置中配置命名空间以避免混乱的导入路径 - 请参阅加载器文档中的 dojoConfig。
另外,考虑使用 dojo/global 模块和/或定义一个 Dojo 模块作为 Underscore.js 的包装器:
//_.js
define(['dojo/global', 'underscore'], function(global){
return global._
});
基于上述考虑,您必须手动加载实际的 .js 文件。如果与 dojo/text 插件结合使用,可以创建一个包装器,该包装器还加载所需的 JS 并对其进行评估,以下可以解决问题。
/var/www/dojo-release-1.7/ext_lib/_.js - 此示例文件分层放置在库命名空间中,与 dojo、dijit、dojox 并排放置
define(['dojo/global', 'dojo/text!./Underscore.js'], function(global, scriptContents){
global.eval(scriptContents);
return global._ // '_' is the evaluated reference from window['_']
/**
* Alternatively, wrap even further to maintain a closure scope thus hiding _ from global
* - so adapt global.eval to simply eval
* - remove above return statement
* - return a dojo declared module with a getInstance simple method
*/
function get_ () { return _ };
var __Underscore = declare('ext_lib._', [/*mixins - none*/], {
getInstance: get_
});
// practical 'static' reference too, callable by 'ext_lib.getInstance' without creating a 'new ext_lib._'
__Underscore.getInstance = get_;
return __Underscore;
});
在此处定义own modules using declare 的示例
注意:此代码未经测试;随时添加更正。