1

f.e. i have:

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

and module1.js

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

i need module1 on demand but i need to run function_1 everytime i requre that module, in this case function_1 in module1 is executed only on first load but i need that routine every time i require module1. only return callback is executed

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

检索函数会带来更多的复杂性,并且在某些情况下它不适用于主干插件和循环依赖项,特别是在需要从头开始重新初始化页面的某些部分的大型 1page 应用程序上,我最终使用了魔术模块

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 回答