0
Backbone.js的用於MV *在前端,RequireJS依賴管理和REST服務,從數據庫中檢索數據。這些服務與網絡應用程序位於同一個域中。
問題
當我經由IE 7 REST服務從服務器獲取的集合,錯誤回調被拋出。當我檢查xhr對象時,狀態爲0,statusText爲0.這只是IE 7中的一個問題。FF和Chrome與ajax GET沒有問題。 IE 7的渲染下,在IE 9使用的開發工具的問題只發生在當
下面是代碼(AMD定義)
define([
'underscore',
'backbone',
'models/imageLink',
'app/registry'
], function(_, Backbone, ImageLink, AppReg) {
var collection = Backbone.Collection.extend({
ajaxRequest: false,
highestKey: 0,
polling : false,
pollDelay: 7500,
// Reference to this collection's model.
model: ImageLink,
url: function(){
return "http://samedomain:4020/api/imageLinks" + "?userId=" + this.user;
},
user: "",
initialize: function() {
// ensure correct context of 'this'
_.bindAll(this, 'startPolling', 'stopPolling', 'executePolling', "onFetch");
var _this=this;
console.log('Image Links collection has been initialized.');
},
// used for sorting collection, sorts based on the lowercase value of the imageLink's text attribute
comparator: function(imageLink) {
return imageLink.get('text').toLowerCase();
},
// override parse function to prevent backbone from updating empty data returned from server
parse: function(response,options) {
//debugger;
if (options.xhr.status===204) {
return this.toJSON();
}
else
return response;
},
getHighestKey: function() {
if (this.length) {
return this.at(this.length-1).get("id");
}
else {
return this.highestKey;
}
},
startPolling : function() {
this.polling = true;
this.highestKey = this.getHighestKey();
this.executePolling();
},
stopPolling : function() {
this.polling = false;
},
executePolling : function() {
if (this.ajaxRequest == "") {
this.ajaxRequest = this.fetch({ reset:true, complete: this.onFetch, timeout: 30000, data: $.param({ key: this.highestKey }),
error: function(model, xhr, options) {
alert("Error\n"+xhr.readyState+"\n"+xhr.status+"\n"+xhr.statusText.toString()+"\n"+xhr.responseText);
}
});
}
},
onFetch : function() {
this.ajaxRequest = "";
this.highestKey = this.getHighestKey();
if(this.polling) {
// poll database
setTimeout(this.executePolling,this.pollDelay);
}
}
});
return collection;
});
注意
頁工作正常單獨加載在IE 7中。
另外,我在執行應用中的以下啓動:
// do not cache any ajax requests
$.ajaxSetup({ cache: false });
// needed for IE CORS support
$.support.cors = true;