使用Requirejs使用Highcharts加载“主题”和“导出”模块

时间:2013-05-15 23:06:36

标签: javascript jquery highcharts requirejs amd

我在requirejs.config函数中尝试了几种不同的路径配置和填充属性,但似乎没有加载我的gray.js主题和exports.js模块。目前我有:

requirejs.config({
    baseUrl: 'static/js',
    paths: {
        'jquery' : 'jquery-1.7.2.min',
        'highcharts' : ['HighCharts/highcharts',
                         'HighCharts/themes/gray',
                         'HighCharts/modules/exporting']
    },
    shim: {
        'highcharts': {
            'exports': 'Highcharts',
            'deps': ['jquery']
         }
    }        
});

我做错了什么?我无法将gray.js和exports.js分解为自己的路径,并将它们作为deps添加到highcharts垫片中,因为它们依赖于highcharts。

我正在考虑让路径中的'highcharts'快捷方式指向exports.js并制作gray.js和highcharts.js deps,但这看起来有点令人困惑。如下所示。想法?

requirejs.config({
    baseUrl: 'static/js',
    paths: {
        'jquery' : 'jquery-1.7.2.min',
        'highcharts' : 'HighCharts/modules/exporting',
        'highcharts-theme': 'HighCharts/themes/gray',
        'highcharts-module': 'HighCharts/highcharts'
    },
    shim: {
        'highcharts-module': {
            'exports': 'Highcharts',
            'deps': ['jquery']
         },
        'highcharts': {
            'deps': ['highcharts-module', 'highcharts-theme']
        }
    }
});

UPDATE:

我的页面html(缩写):

<!DOCTYPE html>
<html lang="en-us" >
   <head>
      <script src="/static/js/require.js" type='text/javascript'></script>
      <script src="/static/js/requirejs.config.js" type='text/javascript'></script>
   </head>
   <body>
      <div id="#myChart"></div>

      <script type="text/javascript"> 
         require(['jquery','domReady', 'highcharts'], function($, domReady, Highcharts){
            domReady(function(){
               //Stuff to draw Chart here
            });
         });
      </script>
   </body>

3 个答案:

答案 0 :(得分:4)

requirejs.config({
    baseUrl: "scripts/",
    paths: {
        "highcharts": "lib/highcharts-src",
        'highcharts-theme': 'lib/dark-unica'
    },
    shim: {
        jquery: {
            exports: 'jQuery'
        },
        highcharts: {
            deps: ['lib/jquery'],
            exports: 'Highcharts'
        },
        'highcharts-theme': {
            deps: ['highcharts'],
            exports: 'Highcharts'
        }
    }
});

这样你可以做到

define(['highcharts-theme'],function( ...

或者如果你需要它而没有主题

define(['highcharts'],function( ...

答案 1 :(得分:1)

稍微简单一些:

requirejs.config({
    baseUrl: 'static/js',
    paths: {
        'jquery' : 'jquery-1.7.2.min',
        'highcharts' : 'HighCharts/modules/exporting',
    },
    shim: {
        'HighCharts/highcharts': {
            'exports': 'Highcharts',
            'deps': ['jquery']
         },
        'highcharts': {
            'deps': ['HighCharts/highcharts', 'HighCharts/themes/gray']
        }
    }
});

答案 2 :(得分:0)

我终于实现了以下解决方案。 @Lyn Headley,您的解决方案无法解决在主题之前需要加载highcharts.js的依赖关系,但再次感谢澄清Paths Fallback语法。

requirejs.config({
    baseUrl: 'static/js',
    paths: {
        'jquery' : 'jquery-1.7.2.min',
        'highcharts' : 'HighCharts/modules/exporting',
        'highcharts-theme': 'HighCharts/themes/gray',
        'highcharts-module': 'HighCharts/highcharts'
    },
    shim: {
        'highcharts-module': {           // This allows users to only import highcharts 
            'exports': 'Highcharts',     //   without theme or exporting module.
            'deps': ['jquery']
         },                                        // Make sure highcharts.js is loaded
        'highcharts-theme': ['highcharts-module'], //   before the theme is.
        'highcharts': {
            'deps': ['highcharts-module', 'highcharts-theme'],
            'exports': 'Highcharts'      // Still gotta make this available
        }
    }
});