jQuery實際上可以通過promises來做到這一點。這只是修改代碼的問題。
假設你自己的代碼和所有生活in the same domain或如果跨域,服務器allows CORS,我們將加載每個.ajax()
。然後,我們將使用.when()
來檢測何時加載所有的承諾,並加入我們的回調來執行所有的承諾解決。
//you can provide a detailed setting for each using .ajax()'s second parameter
//however, jQuery can "smart detect" the content's dataType
var chart = $.ajax(urlOfChart), //script
theme = $.ajax(urlOfTheme), //css
data = $.ajax(urlOfData); //JSON
//we use .when() to check when all three resolves
//the arguments will be the jqXHR objects for the requests
//in the order they were given in the argument list
$.when(chart,theme,data).then(function(chart,theme,data){
//according to jQuery.ajax(), if a script is requested, it is evaluated
//and we still get it's plain text content
//so with respect to the script, we do no processing
//with the css, we get it as plain text since no dataType value handles it
//we embed it into the DOM by creating a style element
//and append the text in it for the styles to take effect on the page
$('<style type="text/css" />').html(theme).appendTo('head');
//as for the JSON, jQuery converts it into an object
//and is passed as an argument as the return data for that promise
//...everything is now requested, retrieved and prepared...
//everything else goes under here
});
對,將數據定義爲模塊也應該起作用,我只是不知道該怎麼做。你能指點我具體的例子還是教程? – Christophe
您的其他模塊是如何定義的?就這樣。例如,對於AMD,你可以調用'define('mydata',{.....})'或類似的東西。 –
我剛接觸AMD,只是使用了來自Dojo Toolkit的開箱模塊。這非常有意義,我只需要學習如何去做。謝謝您的幫助! – Christophe