我正在開發一個Backbone + require應用程序。事情正在發揮作用,但在服務器上更新模型後,儘管服務器返回了200,但傳遞給模型「保存」的選項哈希中的'錯誤'函數被調用。解析沒有在我的模型中調用Backbone
我想我已經發現問題在於服務器返回一個包含'id'的JSON對象,而模型有一個標記爲'aid'的id屬性。
我的理解是,這應該在模型的'parse'函數中處理,但是我無法得到該模型的''parse'函數被調用。這裏是我的模型:
define([
// These are path alias that we configured in our bootstrap
'jquery', // lib/jquery
'underscore', // lib/underscore
'backbone', // lib/backbone
'util'
], function($, _, Backbone){
// Above we have passed in jQuery, Underscore and Backbone
// They will not be accessible in the global scope
var Address = Backbone.Model.extend({
initialize: function() { console.log("Address initialized"); },
urlRoot: '/address/',
parse: function(response, options) {
console.log("In Address::parse");
for(thing in response) {
console.log("Key:" + thing + ", Val: " + response[thing]);
}
}
});
return {
address: Address
};
});
,這裏是我的觀點的相關部分:
events: {
"submit #add-address-form": "addAddress",
},
addAddress: function(ev) {
var that = this;
ev.preventDefault();
var addressDetails = $(ev.currentTarget).serializeObject();
var addr = new A.address();
addr.save(addressDetails, {
success: function(model, response, options) {
that.Backbone.application.router.navigate('', {trigger: true});
},
error: function(model, response, options) {
console.log("Response status: " + response.statusCode());
}
});
return false;
},
當通過視圖提交表單提交「addAddress」被觸發,服務器更新。我的應用程序從服務器接收到200,並且JSON對象 '{id:}',但模型中的解析函數從不會被調用。
任何幫助讚賞;
如果需要,您可以[覆蓋默認id屬性]你的模型(http://backbonejs.org/#Model-idAttribute)。 – bejonbee