需要垫片设置-jquery.flot/jquery.flot.selection

require shim setup- jquery.flot/jquery.flot.selection

本文关键字:flot jquery selection -jquery 设置      更新时间:2023-09-26

所以我正在使用jquery.flot和jquery.flot.selection,由于define({…异步加载模块),我遇到了一个问题,因为选择插件试图将自己推入$.prot.plugins(由jquery.fot创建),但此时$.prot.plugins仍未定义。

我发现require.config中的"shim"参数应该能帮我解决这个问题,但我运气不好。。。下面是概要。。。jquery.flot创建$.plotjquery.flot.selection将自己添加到$.plot.plugins

我尝试过的。。。

shim:{
    'js/lib/jquery.flot':{
        exports:'$.plot'
    },
    'js/lib/jquery.flot.selection':{
        deps:['js/lib/jquery.flot']
    }
}

还有

shim:{
    'js/lib/jquery.flot.selection':['js/lib/jquery.flot']
}

我的插件是这样的。。

define(['jquery','lib/jquery.flot','lib/jquery.flot.selection'], function() {
(function($) {
    // jQuery plugin definition
.....

我也试过

define(['jquery'],function(){
require[('js/lib/jquery.flot.selection'],function(){
//jQuery plugin definition
...

我该怎么办???

对于将来碰巧遇到这种情况的人来说,RequireJS在诊断问题时可能有点不透明。下面是RequireJS 2的一个工作示例,带有jquery.flot和jquery.flot.selection,没有use模块。

很难说这个例子和上面的问题有什么不同,所以不透明!

require.config({
    shim: {
        'jquery': {
            exports: '$'
        },
            'jquery.flot': {
            deps: ['jquery'],
            exports: '$.plot'
        },
            'jquery.flot.selection': {
            deps: ['jquery.flot']
        }
    },
    paths: {
        'jquery': '//cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min',
            'jquery.flot': '//cdnjs.cloudflare.com/ajax/libs/flot/0.8.1/jquery.flot.min',
            'jquery.flot.selection': 'https://raw.github.com/flot/flot/master/jquery.flot.selection'
    }
});
require(['jquery', 'jquery.flot', 'jquery.flot.selection'], function ($) {
    // jQuery plugin definition
    console.log($.plot);
    $('#x').text($.plot.plugins);
});