【问题标题】:How to mixin Underscore plugins in RequireJS?如何在 RequireJS 中混入 Underscore 插件?
【发布时间】:2012-11-29 13:57:44
【问题描述】:

加载时在 Underscore 上执行代码的正确方法是什么?我正在尝试执行以下代码以在模块需要时自动扩展 _ 导出的命名空间:

_.mixin(_.str.exports());

文档有点含糊,但我想我把它放在了 shim init 部分?我尝试了以下方法,但我什至无法在 init 中找到断点:

require.config({
    paths: {
        jquery: 'libs/jquery/jquery.min',
        underscore: 'libs/underscore/lodash.min',
        underscorestring: 'libs/underscore/underscore.string.min'
    },

    shim: {
        underscore: {
            exports: '_'
        }
        underscorestring: {
            deps: ['underscore'],
            init: function (_) {
                //Mixin plugin to namespace
                _.mixin(_.str.exports());

                return _;
            }
        }
    }
});

当我尝试这样做并使用下划线时,我得到了这个错误:

Uncaught TypeError: Object function s(e){return new o(e)} has no 方法'startsWith'

文档:

【问题讨论】:

    标签: javascript asynchronous requirejs underscore.js dynamic-script-loading


    【解决方案1】:

    我不知道这是否是正确的方法,但我通过反转事物来使其工作,以便下划线依赖于 underscore.string。此外,这样您就不必要求 underscore.string。

    require.config({
      shim: {
        'backbone': {
          deps: ['underscore', 'jquery'],
          exports: 'Backbone'
        },
        'underscore': {
          deps: ['underscore.string'],
          exports: '_',
          init: function(UnderscoreString) {
            _.mixin(UnderscoreString);
          }
        }
      },
      paths: {
        'backbone'          : 'lib/backbone',
        'jquery'            : 'lib/jquery/jquery',
        'text'              : 'lib/require/text',
        'underscore'        : 'lib/underscore',
        'underscore.string' : 'lib/underscore.string'
      }
    });
    

    .

    更新:2014 年 3 月 14 日

    Underscore.js v1.6.0 恢复了 AMD 兼容性,并且 init() 已从 RequireJS 中删除,因此需要进行一些重构。为了继续使用 Underscore.string 预加载 Underscore,我制作了一个混音器模块将它们拉到一起。

    新的 Require.js 配置

    requirejs.config({
      paths: {
        'backbone'            : '../lib/backbone/backbone',
        'backbone.base'       : '../lib/backbone/backbone.base',
        'backbone.extensions' : '../lib/backbone/backbone.extensions',
        'jquery'              : '../lib/jquery/jquery',
        'text'                : '../lib/require/text',
        'underscore'          : '../lib/underscore/underscore',
        'underscore.mixed'    : '../lib/underscore/underscore.mixed',
        'underscore.string'   : '../lib/underscore/underscore.string'
      },
      shim: {
        'backbone.base': {
          deps: ['underscore.mixed', 'jquery'],
          exports: 'Backbone'
        },
      }
    });
    

    underscore.mixed

    define([
      'underscore',
      'underscore.string'
    ], function(_, _s) {
      _.mixin(_s.exports());
      return _;
    });
    

    最后一步是在模块定义中用'underscore.mixed' 替换所有'underscore' 实例。我尝试将 Underscore 移动到一个名为 underscore.base.js 的文件中,并将常规的 underscore 设置为混音器(如 Backbone 设置)以避免此步骤。 Underscore,作为一个命名的模块,不同意这个计划。

    【讨论】:

    【解决方案2】:

    你在某处需要下划线吗?因为如果它不是必需的,它将不会被加载。 我设法让它使用您发布的几乎完全相同的代码:

    require.config({
        paths: {
            underscore: [
                '//raw.github.com/documentcloud/underscore/master/underscore-min'
            ,   'lib/underscore'
            ]
        ,   underscorestring: 'https://raw.github.com/epeli/underscore.string/master/dist/underscore.string.min'
        }
    ,   shim: {
            underscore: { exports: '_' },
            underscorestring: {
                deps: ['underscore'],
                init: function(_) { 
                    _.mixin(_.str.exports());
                    return _; // guess, this is not needed.
                }
            }
        }
    ,   exclude: ['underscore']
    });
    
    require(['underscore', 'underscorestring'], function(_) {
        console.log( _.chars("i'm a happy string.") );
    });
    

    【讨论】:

      【解决方案3】:

      在我明白我做错了什么之前与这个斗争了几个小时

      这是我做错了

      你不应该在 main.js 中重命名文件 underscore.string

      即使在我的库中我确实将路径中的文件重命名为'underscore.string'

      这就是你的 ma​​in.js 的样子

      require.config({
      paths: {
          underscore: 'lib/underscore', 
          'underscore.string' : 'lib/_string' ,
      },
      shim: { 
          underscore: {
              exports: '_', 
              deps: [ 'jquery', 'jqueryui' ]
          }, 
          'underscore.string': { 
              deps: [ 'underscore' ]
          },
      } 
      ....
      

      然后您可以将其添加为 dependency 在您的 shim 中,就像我为我的 mixin 文件所做的那样

      shim: { 
          mixin : {
              deps: [ 'jquery',  'underscore', 'underscore.string' , 'bootstrap'  ] 
          },  
      

      或者只是在您的不同页面中定义它,例如

      /*global define */
      define([    
          'underscore.string'
      ], function ( ) {   
      

      它现在可以工作了,你可以通过 _.str 或 _.string 访问它

      这就是为什么您应该这样做,而不是尝试将其命名为其他名称

      underscore.string.js 的第 663 行

        // Register as a named module with AMD.
        if (typeof define === 'function' && define.amd)
          define('underscore.string', [], function(){ return _s; });
      

      这意味着如果你定义'underscore.string',它只会在AMD需要JS中注册它

      对于混入你可以只定义

       /*global define */
      define([     
          'underscore',
          'underscore.string'
      ], function ( ) {   
        _.mixin(_.str.exports());
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-01-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-10
        • 2016-06-22
        • 1970-01-01
        • 2012-05-17
        相关资源
        最近更新 更多