Javascript 如何在RequireJS中混合下划线插件?
加载下划线时,在下划线上执行代码的正确方法是什么?我正在尝试执行以下代码,以便在模块需要时自动扩展导出的命名空间:Javascript 如何在RequireJS中混合下划线插件?,javascript,asynchronous,requirejs,underscore.js,dynamic-script-loading,Javascript,Asynchronous,Requirejs,Underscore.js,Dynamic Script Loading,加载下划线时,在下划线上执行代码的正确方法是什么?我正在尝试执行以下代码,以便在模块需要时自动扩展导出的命名空间: _.mixin(_.str.exports()); 文档有点模糊,但我想我把它放在shim init部分了?我尝试了以下方法,但我甚至无法在init中找到断点: require.config({ paths: { jquery: 'libs/jquery/jquery.min', underscore: 'libs/underscore/l
_.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 _;
}
}
}
});
当我尝试执行此操作并使用下划线字符串时,会出现以下错误:
未捕获类型错误:对象函数s(e){返回新的o(e)}没有
方法“startsWith”
文件:
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.") );
});
我不知道这是否是正确的方法,但我通过反转使下划线依赖于下划线.string来实现。此外,这样您就不需要下划线.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日
Underline.js v1.6.0恢复了AMD兼容性,并且已从RequireJS中删除了init()
,因此需要进行一些重构。为了继续使用下划线.string预加载下划线,我制作了一个混音器模块来将它们拉到一起
新Require.js配置
下划线。混合
最后一步是将模块定义中的所有
'underline'
实例替换为'underline.mixed'
。我尝试将下划线移动到名为subline.base.js
的文件中,并将常规的下划线
设置为混音器(如主干设置)以避免此步骤。下划线,作为一个命名模块,不同意该计划。在我明白自己做错了什么之前,我为此奋斗了数小时
这就是我做错的
不应重命名main.js中的下划线.string文件
即使在我的库中,我确实在路径中重命名了文件,但我将其重新命名为“下划线.string”
这就是您的main.js的外观
require.config({
paths: {
underscore: 'lib/underscore',
'underscore.string' : 'lib/_string' ,
},
shim: {
underscore: {
exports: '_',
deps: [ 'jquery', 'jqueryui' ]
},
'underscore.string': {
deps: [ 'underscore' ]
},
}
....
然后,您可以将其作为依赖项添加到您的垫片中,就像我对mixin文件所做的那样
shim: {
mixin : {
deps: [ 'jquery', 'underscore', 'underscore.string' , 'bootstrap' ]
},
或者在不同的页面中定义它,如
/*global define */
define([
'underscore.string'
], function ( ) {
它现在就可以工作了,您可以通过u.str或u.string访问它
这就是为什么你应该这样做,而不是试着给它取个别的名字
在下划线.string.js的第663行
// Register as a named module with AMD.
if (typeof define === 'function' && define.amd)
define('underscore.string', [], function(){ return _s; });
这意味着,如果您正在定义“下划线.string”,它将只向AMD require JS注册它
对于混入,您可以使用定义
/*global define */
define([
'underscore',
'underscore.string'
], function ( ) {
_.mixin(_.str.exports());
在最新的RequireJS中,“init”似乎不起作用。检查一下这种技巧:您可以使用
map
属性有效地将“下划线”别名为“下划线.mixed”。看见
// Register as a named module with AMD.
if (typeof define === 'function' && define.amd)
define('underscore.string', [], function(){ return _s; });
/*global define */
define([
'underscore',
'underscore.string'
], function ( ) {
_.mixin(_.str.exports());