使用requireJS的插件,出口多个功能的jQuery [英] using requireJS for plugin that exports multiple function to jquery
问题描述
我试图用requireJS添加这个库/插件来的jQuery。
I'm trying to use requireJS to add this library/plug-in to jquery.
我看了看文档和其它S.O.问题,我仍然无法弄清楚发生了什么事情错了。
I've looked at the documentation, and other S.O. questions, and I still can't figure out what's going wrong.
使用骨架和要求,例如:
我是pretty知道我需要在我main.js文件中使用垫片。
我也pretty肯定,我需要还加载插件在特定的文件我想用它(即使我不实际使用的rangyInputs在函数体对象)(其我为骨干网做的)。真的,我只是想从rangyInputs库jQuery的绑定功能一次,然后只需要在jQuery的,我需要这些功能的所有文件。
Using backbone and require as an example: I'm pretty sure I need to use shim in my main.js file. I'm also pretty sure that I need to still load the plug-in in the particular file I want to use it in (Even though I don't actually use the rangyInputs object in the body of the function) (which I do for backbone). Really, I just want to bind the functions from the rangyInputs library to jquery once, and then just require jquery in all the files I need those functions.
define([
'jquery',
'underscore',
'backbone'
, 'views/listView'
, 'sockets'
, 'collections/nodesCollection'
, 'views/listView'
, 'models/node'
, 'rangyInputs' //I define the path to this like I do the path for backbone in main.js
],
function($, _, Backbone, ListView, io, nodesCollection, listView, nodeModel ,rangyInputs){
在未缩小的图书馆中有本底:
The un-minified library has this at the end:
$.fn.extend({
getSelection: jQuerify(getSelection, false),
setSelection: jQuerify(setSelection, true),
collapseSelection: jQuerify(collapseSelection, true),
deleteSelectedText: jQuerify(deleteSelectedText, true),
deleteText: jQuerify(deleteText, true),
extractSelectedText: jQuerify(extractSelectedText, false),
insertText: jQuerify(insertText, true),
replaceSelectedText: jQuerify(replaceSelectedText, true),
surroundSelectedText: jQuerify(surroundSelectedText, true)
});
的文档,并给出在哪里使用出口为隔片的例子,但我不认为我可以用这个,因为我有多个事情我出口。 (我不完全知道如何出口工程)。
The documentation and gives examples where of using "exports" for shim, but I don't think I can use this since I have multiple things I'm exporting. (and I'm not exactly sure how exports works).
谢谢!
编辑:我试着将这个解决方案,这样我可以包括不同的插件,并且它没有工作,所以我问<一个href=\"http://stackoverflow.com/questions/20937259/requirejs-worked-for-one-plug-in-but-not-another\">another这里问题
I tried applying this solution so that I could include a different plug-in, and it didn't work, so I asked another question here
推荐答案
成功地创造出简单的例子:
Managed to create simple working example:
index.html的:
<!doctype html>
<html>
<head></head>
<body>
<script data-main="main" src="require.js"></script>
</body>
</html>
main.js:
require.config({
paths : {
jquery : 'jquery-2.0.3',
'rangyinputs-jquery' : 'rangyinputs-jquery-1.1.2'
},
shim : {
'rangyinputs-jquery' : {deps : ['jquery'], exports : '$'}
}
});
require(['jquery', 'rangyinputs-jquery'], function($) {
console.log('Type of $.fn.getSelection' typeof $.fn.getSelection);
});
的jQuery
最后版本是AMD兼容,所以你不必使用出口垫片,但rangyinputs,jQuery是没有,所以我们出口jQuery对象,并添加依赖jQuery的。
在一个目录下的所有文件:
last versions of jquery are AMD compatible, so you don't have to use exports shim, but rangyinputs-jquery is not, so we export jquery object and add dependency to jquery. all files in one directory:
- 的index.html
- main.js
- jQuery的-2.0.3.js
- rangyinputs-的jQuery-1.1.2.js
- require.js
这篇关于使用requireJS的插件,出口多个功能的jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!