2013-05-10 52 views
0

after reading this hack爲什麼requirejs不加載依賴關係?

它清除了jquery在依賴關係之前加載的概念。 我調整了它的requrejs。控制檯沒有錯誤。 但兩個js文件都被加載(fyjsfies)。

但不是requrejs依賴關係。

我做了我的代碼是這樣的..

(function (window, document, callback) { 
    var j, d; 
    var loaded = false; 
    if(!(j = window.require) || callback(j, loaded)) { 
    var script = document.createElement("script"); 
    script.type = "text/javascript"; 
    script.src = "full_https_path_myjsfile.js"; 
    script.onload = script.onreadystatechange = function() { 
     if(!loaded && (!(d = this.readyState) || d == "loaded" || d == "complete")) { 
     callback((j = window.require).noConflict(1), loaded = true); 
     j(script).remove(); 
     } 
    }; 
    var script = document.createElement('script'); 
    script.type = 'text/javascript'; 
    script.src = "myjsfile.js"; 
    $("#someElement").append(script); 
    } 
})(window, document, function ($, require_loaded) { 
    // Widget code here 
    alert('i m here'); 
    require(["jquery", "model2", "model3", "model4"], function ($, model1, model2, model3) {}); 
}); 

警報不會被調用,爲什麼???

回答

2

你不需要這個hack來加載jQuery或其他任何事情。 You can configure RequireJS將依賴項別名替換爲路徑,特別是那些未針對RequireJS格式化的依賴項。

require.config({ 
    paths : { 
    'jQuery' : 'path/to/jQuery.min.js' 
    } 
}); 

require(['jQuery'],function($){ 
    //jQuery is loaded 
    //$ is either the global jQuery or jQuery if it was AMD formatted 
}); 
+0

對不起!通過這個script.src =「myjsfile.js」;我的意思是script.src =「fullpath_to_myjsfile.js」;但它仍然不起作用 – 2013-05-10 11:24:39

0

問題是require腳本被添加到DOM但未加載。因此onReadyStateChanged未被解僱。要解決這個問題,請使用document.documentElement.childNodes[0].appendChild(script)代替