4
我正在構建一個使用.CSV文件作爲Backbone Model的數據源的應用程序。覆蓋同步以使用dataType「text」而不是「json」的最佳方法是什麼?尋找最佳實踐等...Backbone.sync覆蓋dataType
不幸的是,Backbone.sync使用的「dataType」參數是硬編碼的......並且不會響應options.dataType。有問題的代碼片段可以在帶註釋的源代碼中看到 - http://backbonejs.org/docs/backbone.html#section-166
這是我在模型上創建的同步方法。其中大部分是直接從Backbone.sync複製粘貼。我的模型也是隻讀的。
sync: function(method, model, options){
//overwrite sync to read a .CSV document. if
//the default Backbone sync would let you
//specify the "dataType" property, this wouldn't
//be necessary.
if(method === 'read'){
options || (options = {});
var success = options.success;
options.success = function(resp, status, xhr) {
if (success) success(json, status, xhr);
};
var params = {type: 'GET', dataType: 'text', url: this.url};
return $.ajax(_.extend(params, options));
}
},
我已經覆蓋「解析」來處理CSV響應。
parse: function(data, xhr){
return $jQuery.parseJSON(CSVParser.toJSON(data));
},