问题描述
什么是当它被加载到执行上下划线code的正确方法?我想执行下面的code当模块需要它自动延长_出口命名空间:
What is the right way to execute code on Underscore when it gets loaded? I am trying to execute the below code to extend the _ exported namespace automatically when modules require it:
_.mixin(_.str.exports());
该文档是有点模糊,但我想我把它放在垫片INIT部分?我想下面的,但我不能连得断点在init打:
The docs are a bit vague but I think I put it in the shim init section? I tried the below but I can't even get a breakpoint to hit in the 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 _;
}
}
}
});
当我尝试这样做,并使用underscorestring,我得到这个错误:
When I try to do this and use underscorestring, I get this error:
遗漏的类型错误:对象函数s(五){返回新O(E)}无
方法startsWith
文件:
推荐答案
我不知道这是否是正确的做法,但我得到了它通过使下划线取决于underscore.string反相工作的事情。此外,这样你就不必要求underscore.string。
I don't know if it's the correct way, but I got it working by inverting things so that underscore depends on underscore.string. Also, this way you don't have to require 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删除,所以一些重构是为了。要继续获得下划线preloaded与Underscore.string我做了一个混频器模块拉在一起。
Underscore.js v1.6.0 brought back AMD compatibility and init()
has been removed from RequireJS, so some refactoring is in order. To continue getting Underscore preloaded with Underscore.string I made a mixer module to pull them together.
新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.base.js
命名一个文件,从而使常规强调
混频器(如骨干设置),以避免这一步。强调,作为一个的命名的模块,不同意该计划。
The final step is to replace all instances of 'underscore'
with 'underscore.mixed'
in module definitions. I attempted moving Underscore into a file named underscore.base.js
and making the regular underscore
the mixer (like the Backbone setup) to avoid this step. Underscore, being a named module, disagreed with the plan.
这篇关于如何混入下划线插件,RequireJS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!