我傳遞給我的骨架模型JSON對象。我在骨幹模型中有一個解析函數來轉換一些傳入的屬性。問題是當我獲取這個模型時,這些屬性沒有被分析,只是添加到模型中。底部的圖像顯示,不是將密碼轉換爲密碼而是刪除密碼,它只是將密碼添加到對象的屬性中。
這裏是我的代碼:
JSON
當我使用郵遞員打電話給我的web服務,我得到了響應:
{"type":null,"idTeacher":1,"name":"Sean","password":"tst","email":null,"dob":1392940800000}
型號:
window.Teacher = Backbone.Model.extend({
urlRoot: "http://localhost:8080/SIMS/resource/teacher",
defaults: {
"id": null,
"Name": "",
"Password": "",
"email": "",
"dob": "",
"type": ""
},
parse: function(response){
response.id = response.idTeacher;
response.Password = response.password;
response.Name = response.name;
delete response.name;
delete resoponse.password;
delete response.idTeacher;
return response;
}
});
window.TeacherCollection = Backbone.Collection.extend({
model: Teacher,
url: "http://localhost:8080/SIMS/resource/teacher",
parse: function(response){
return response;
}
});
Main.js //這是
before: function(callback) {
if (this.teacherList) {
if (callback) callback();
} else {
console.log('........................................javascript........');
this.teacherList = new TeacherCollection();
console.log('Loading List: Size: ' + this.teacherList.length);
this.teacherList.fetch({success: function() {
console.log('........... ftech success...........');
$('#contents').html(new TeacherListView({model: app.teacherList}).render().el);
if (callback) callback();
}});
}
}
如果調試我的骨幹,我可以看到我的解析沒有任何分析的變量和解析刪除通話也不能工作。
UDATE ANSWER
感謝您的幫助。我沒有收集類中的代碼是一個問題。但第二個原因是我沒有循環收集來改變每個屬性。