AMD和Dojo 1.7的问题

时间:2012-07-15 17:27:31

标签: dojo amd

简单的问题。

AMD DOJO实现是否支持这些类型的声明?

  1. 文本!./ plain.html

    define(["../Widget","text!./plain.html"],
      function(Widget,plain){
      return new Widget({name:"mainApp",template:plain});
    }); 
    
  2. 加载非模块,比方说underscore.js

    require(['dir/underscore.js'], function(){
     _.reduce ...
    });
    

1 个答案:

答案 0 :(得分:2)

是的,但确切的语法与问题中使用的语法不同。

加载字符数据的插件是dojo/text

加载JavaScript库时不应使用扩展名,并且通过dojoConfig中的dojotoolkit base或 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的示例 注意:此代码未经测试;随意添加更正。