我使用jQuery的每個函數循環參數。以及我推動從「get」方法到數組的所有數據。一旦我的陣列已準備就緒,我需要調用一個函數骨幹..當和應用功能(jQuery)不能正常工作
做我這樣做:
var temps = [];
$.each(views,function(i,view){
$.get("js/temp/" + view + ".html", function(data){
temps.push(data);
console.log(temps); //results correctly.
})
})
$.when.apply(null,temps).done(function(){
console.log(temps); //results as undefined
});
,但我得到的結果爲「未定義」 ..這裏有什麼問題。有人找我嗎?
進一步增加的問題,使用的答案我做到了..
$.each(views,function(i,view){
var d = $.Deferred();
requests.push(d.promise());
$.get("js/temp/" + view + ".html", function(data){
view.prototype.template = _.template(data);//not working when i use the string parameter..
appView.prototype.template = _.template(data); //it works
temps.push(data);
d.resolve();
})
})
如何將字符串參數轉換爲返回功能..?
是的,它工作正常。但是沒有縮短的方法來做到這一點? – 3gwebtrain
其實$ .get也會返回延遲對象,但是如果你需要首先從所有響應中收集數據,這是一個最佳方式 – claustrofob
我現在正在接受另一個問題..實際上使用參數字符串,我需要一個函數,並且需要分配原型..如何將字符串轉換爲對象..?我更新了這個問題。 – 3gwebtrain