这个答案类似于user1903890,但我认为它很容易遵循使其他实现。
基本上我们必须在init函数中封装index.html中指定的main.js requirejs控制器。一旦它的定义,那么我们称这个初始化函数给init requirejs正常
function init_requirejs(){
console.log("--------------------------- INIT requirejs:");
require([ "helpers/util"], function(util) {
var count=0;
$('#content').empty();
$('#content').append("<input type='button' id='increment_button' value='click to increment the counter'>");
$('#content').append("<h1 id='the_counter'>0</h1>");
$('#content').append("<br><br><input type='button' id='init_button' value='click to initialize requirejs files'>");
$('#increment_button').on('click', function(){
count++;
$('#the_counter').text(count);
});
$('#init_button').on('click', function(){
end();
init_requirejs();
});
util();
});
};
init_requirejs();
我们还需要和使用require.onResourceLoad功能来存储所有参加requirejs应用
var all=[];
require.onResourceLoad = function (context, map, depArray) {
all.push(map.name);
};
文件我们需要一个复位requirejs配置功能删除requirejs的实际情况,我们将与require.undef功能做到这一点:
function end(){
console.log("--------------------------- END requirejs:");
all.map(function(item){
require.undef(item);
});
};
这一切!
后来从我们的代码中,我们可以强制重载应用程序,而无需重载浏览器只调用end()函数和init_rquirejs()函数。例如jQuery的单击事件中:
$('#init_button').on('click', function(){
end();
init_requirejs();
});
演示的代码是 https://github.com/juanantonioruz/requirejs-force-reload
和在线版本 https://dl.dropboxusercontent.com/u/8688858/requirejs-force-reload/project.html
我希望这个解决方案为你工作!
胡安
嗨,秦,你检查我的回应?我认为它解决了你的问题,不是吗? – tangrammer 2013-10-21 12:11:49