2

jqplotを requireJS モジュールとして読み込もうとしています。

main.jsのパスとシムは次のようになります。

require.config({
  , paths: {
      plot:          '../js/plugins/jqplot/jqplot.module'
  } 
  , shim: {
    'plot':          { deps: ['jquery']}
  }
});

このモジュールはほとんどのページで必要ないため、pageXYZがロードされるのを待ってから、 a 内で<script></script>呼び出しています:

require(['plot'], 
    function (plot) {
       // do stuff
    }
);

そして、私は次のようにjqplot.module見えます:

define(['../js/plugins/jqplot/jquery.jqplot'],
    function () {
        require([
                '../js/plugins/jqplot/plugins/jqplot.barRenderer'
              , '../js/plugins/jqplot/plugins/jqplot.logAxisRenderer'
              , '../js/plugins/jqplot/plugins/jqplot.categoryAxisRenderer'
              , '../js/plugins/jqplot/plugins/jqplot.canvasAxisTickRenderer'
              , '../js/plugins/jqplot/plugins/jqplot.canvasTextRenderer'
              , '../js/plugins/jqplot/plugins/jqplot.pointLabels'
              , '../js/plugins/jqplot/plugins/jqplot.enhancedLegendRenderer'
        ], 
        function (){
          return $.jqplot;
        }
    );
  }
);

すべてのサブプラグインが定義され、使用可能な正しいオブジェクトを返します。

ただし、Do stuff code runs BEFORE jqplot is assigned to$であるため、コードを実行すると未定義のエラーが引き続き発生します(ファイルがすべてロードされているため、requirejs の実行が開始されると思います)。

質問:
jqplot が $ に割り当てられるまで、コードの実行を停止するにはどうすればよいですか?

4

2 に答える 2

3

1)あなたの質問からこの行を考えます:

define(['../js/plugins/jqplot/jquery.jqplot'],

読む必要があります:

define(['../js/plugins/jqplot/jquery.module'],

shim2)をロードするモジュール定義があるため、は必要ありませんjqplot。だからあなたrequirejs.configをに変えてください

require.config({
  , paths: {
      plot:          '../js/plugins/jqplot/jqplot.module'
  } 
});

3) jqplot.module は現在何も返していません。次のように変更します。

define([
      '../js/plugins/jqplot/jquery.jqplot'
    ],
    function () {
        var plot;
        require([
            '../js/plugins/jqplot/plugins/jqplot.barRenderer',
            '../js/plugins/jqplot/plugins/jqplot.logAxisRenderer',
            '../js/plugins/jqplot/plugins/jqplot.categoryAxisRenderer',
            '../js/plugins/jqplot/plugins/jqplot.canvasAxisTickRenderer',
            '../js/plugins/jqplot/plugins/jqplot.canvasTextRenderer',
            '../js/plugins/jqplot/plugins/jqplot.pointLabels',
            '../js/plugins/jqplot/plugins/jqplot.enhancedLegendRenderer'
            ],
        function () {
            plot = $.jqplot;
        });
        return plot;
    });

これはすべてテストされていませんが、これらが役立つはずです

于 2013-04-27T04:38:14.393 に答える