Requirejs加载插件

发布于 2024-10-24 03:38:02 字数 749 浏览 4 评论 0原文

我正在尝试使用 requirejs 加载插件,但偶尔会出现错误,“$.fn 未定义”,但如果我重新加载页面,错误就会消失,就像现在 jquery 被缓存一样,问题就被消除了。我正在像这样加载我的库:

require(["jquery-1.4", "/script/jquery.autoSuggest.packed.js"], function($) {
   $(function() {
       //code
    });
});

您能看看此实现是否有任何问题会导致此错误吗? Require js 被添加到页面中,如下所示:

<script type="text/javascript" src="http://website.co.uk/frameworks/requirejs/0.2.4/sharedmodules/require.js">
</script> 
<script type="text/javascript">  requireMap = {
     "jquery-1.4":"http://website.co.uk/sharedmodules/jquery-1.4"
}; 

    require({ baseUrl: 'http://website.co.uk/', paths: requireMap });      
</script>

这是无法更改的,因为它是我正在使用的框架的一部分。

有什么建议吗?

谢谢!

I'm trying to load a plugin using requirejs but occasionally get an error, "$.fn is undefined" but if I reload the page the error disappears almost like now jquery is cached the problem is removed. I'm loading my libraries like this:

require(["jquery-1.4", "/script/jquery.autoSuggest.packed.js"], function($) {
   $(function() {
       //code
    });
});

Can you see if there is anything wrong with this implementation that would cause this error? Require js is being added to the page as so:

<script type="text/javascript" src="http://website.co.uk/frameworks/requirejs/0.2.4/sharedmodules/require.js">
</script> 
<script type="text/javascript">  requireMap = {
     "jquery-1.4":"http://website.co.uk/sharedmodules/jquery-1.4"
}; 

    require({ baseUrl: 'http://website.co.uk/', paths: requireMap });      
</script>

This can't be changed as it's part of the framework I'm using.

Any suggestions?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

焚却相思 2024-10-31 03:38:02

订单插件已删除。使用填充程序加载如下

requirejs.config({
   paths: {
        'jquery': 'https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min',
        'bootstrap': '../bootstrap/js/bootstrap.min',
        'select2': 'vendor/select2',
        'jshashtable': 'vendor/jshashtable-2.1',
        'jquery.numberformatter': 'vendor/jquery.numberformatter-1.2.3.min',
        'jq-datepicker': 'vendor/bootstrap-datepicker',
        'jq-datepicker.da': 'vendor/bootstrap-datepicker.da'
    }, 

    // Use shim for plugins that does not support AMD
    shim: {
        'bootstrap': ['jquery'],
        'select2': ['jquery'],
        'jq-datepicker': ['jquery'],
        'jshashtable': ['jquery'],
        'jquery.numberformatter': ['jquery', 'jshashtable']
    },

});

参考: http://requirejs.org/docs/api.html#config-shim

order plugin is removed. use shim loading as follows

requirejs.config({
   paths: {
        'jquery': 'https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min',
        'bootstrap': '../bootstrap/js/bootstrap.min',
        'select2': 'vendor/select2',
        'jshashtable': 'vendor/jshashtable-2.1',
        'jquery.numberformatter': 'vendor/jquery.numberformatter-1.2.3.min',
        'jq-datepicker': 'vendor/bootstrap-datepicker',
        'jq-datepicker.da': 'vendor/bootstrap-datepicker.da'
    }, 

    // Use shim for plugins that does not support AMD
    shim: {
        'bootstrap': ['jquery'],
        'select2': ['jquery'],
        'jq-datepicker': ['jquery'],
        'jshashtable': ['jquery'],
        'jquery.numberformatter': ['jquery', 'jshashtable']
    },

});

reference : http://requirejs.org/docs/api.html#config-shim

心的位置 2024-10-31 03:38:02

您将需要使用订单插件。默认情况下,RequireJS 会尽快加载脚本,并且它们可能会乱序加载。但是,顺序插件将保持脚本加载的顺序。

如果您使用优化器来优化脚本,请确保在优化的脚本中包含 jQuery 和插件。

You will want to use the order plugin. By default RequireJS loads scripts as fast as possible, and they can load out of order. However, the order plugin will maintain the order of the script loads.

If you use the optimizer to optimizer your scripts, be sure to include both jQuery and the plugin in the optimized script.

小帐篷 2024-10-31 03:38:02

您可能需要查看 https://github.com/jquery-boilerplate/patterns

它有一些很好的样板 jQuery 插件代码,可以与 RequireJS 一起使用。

我认为您可能必须将 jQuery 升级到 1.7,然后这些模式才能为您工作。

You may want to checkout https://github.com/jquery-boilerplate/patterns

It has some good boilerplate jQuery plugin code that will work with RequireJS.

I think you may have to upgrade your jQuery to 1.7 before these patterns will work for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文