2

我在 requirejs.config 函数中尝试了几种不同的路径和垫片属性配置,但似乎都没有加载我的 gray.js 主题和 exporting.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 和 exporting.js 分解到它们自己的路径中并将它们作为 deps 添加到 highcharts 垫片中,因为它们依赖于 highcharts。

我正在考虑使路径中的“highcharts”快捷方式指向 exporting.js 并使其成为 gray.js 和 highcharts.js 的依赖项,但这似乎有点令人困惑。像下面这样。想法?

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']
        }
    }
});

更新:

我的页面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>
4

3 回答 3

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( ...
于 2015-01-08T21:46:40.083 回答
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']
        }
    }
});
于 2013-05-16T00:13:48.470 回答
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
        }
    }
});
于 2013-05-16T17:56:32.593 回答