Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

So I'm working with jquery.flot and jquery.flot.selection and since define({... loads modules asynchronously I am having a problem because the selection plugin is trying to push itself into $.plot.plugins (which is created by jquery.flot) but at that moment $.plot.plugins still isn't defined.

I found that the "shim" argument in require.config should help me with this but I am having no luck... so here's the rundown... jquery.flot creates $.plot and jquery.flot.selection adds itself to $.plot.plugins

what I've tried...

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

also

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

my plugin looks like this..

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

I also tried

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

What should I do???

like image 408
Joe Avatar asked Sep 13 '12 14:09

Joe


1 Answers

For anyone who happens to run into this in the future, RequireJS can be a little opaque when it comes to diagnosing problems. Here's a working example of RequireJS 2 with jquery.flot and jquery.flot.selection, without the use module.

It's a little hard to tell what's different between this example and the question above, thus the opaqueness!

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);
});
like image 200
Chris Avatar answered Sep 19 '22 01:09

Chris