1

私は持っています:

require( ['module1'], function( module1_callback){
//callback_1 after code is loaded
} );

およびmodule1.js

define( ['module2'], function(module2_callback)
{
    //function_1 to perform something
    return 'something';
});

オンデマンドでモジュール 1 が必要ですが、そのモジュールが必要になるたびに function_1 を実行する必要があります。この場合、module1 の function_1 は最初のロード時にのみ実行されますが、module1 が必要になるたびにそのルーチンが必要です。return コールバックのみが実行されます

4

2 に答える 2

2

これは、requirejs が機能する方法ではありません。ただし、何かを実行してから何かを返す関数を返すことはできます。

define( ['module2'], function(module2_callback) {
  return function() { 
    //to perform something
    return 'something';
  }
});

そしてあなたのベースモジュールで:

require( ['module1'], function( module1_callback){
   var something = module1_callback();
});
于 2013-01-22T18:34:38.910 に答える
-2

関数を取得すると複雑さが増し、特にページの一部を最初から再初期化する必要がある大規模な1ページアプリでは、バックボーンプラグインと循環依存関係で機能しない場合があり、魔法のモジュールを使用することになります

https://github.com/jrburke/requirejs/wiki/Differences-between-the-simplified-CommonJS-wrapper-and-standard-AMD-define#wiki-magic

undef

define( ['module2', 'module'], function(module2_callback) {
     //push module id into global array 
     window.undefArr.push(module);
    //all code here will be executed on every require
});

//before require undef modules
//here you can control which modules undef which to leave in memory
//based on module properties like path, name, status etc.
_.each( window.undefArr, function(mod){
   requirejs.undef(mod.id);
});
//init undefArr
undefArr = [];
require( ['module1']);
于 2013-01-22T19:59:11.607 に答える