【问题标题】:AMD and Dojo 1.7 questionsAMD 和 Dojo 1.7 问题
【发布时间】:2012-07-15 17:27:31
【问题描述】:

简单的问题。

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 ...
    });
    

【问题讨论】:

    标签: dojo amd


    【解决方案1】:

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

    加载字符数据的插件是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 的示例 注意:此代码未经测试;随时添加更正。

    【讨论】:

    • 注意本文中的第一个要求;通过只告诉顶级命名空间需要 - 必须提供包“下划线”的正确声明,遵循“Dojo 的包概念”,这意味着“下划线”是一个目录,它包含一个包描述符(参见 CommonJS)
    • 它对我有用(使用你的第一种方法)。我尝试使用 AMD 支持的 underscorejs fork,如下所述:stackoverflow.com/questions/8131265/…。它对我来说并没有立即起作用,所以我决定采用这种方法。有人能用 Dojo AMD 加载程序加载这个 AMD 下划线分支吗?
    猜你喜欢
    • 2012-02-29
    • 2012-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-10
    • 1970-01-01
    相关资源
    最近更新 更多