在我的Marionette.CompositeView
中,可以創建向服務器(1)發出放入請求的新模型。
放入請求是好的,但是當我將新模型添加到集合中時,新模型會忽略由服務器創建的ID。
我應該如何解決這個問題?如何在創建保存新模型時獲取ID
應該
1)POST請求發送ID到客戶端或
2)我必須作出從客戶的請求,以獲得ID?
(1)
return Marionette.CompositeView.extend({
submitForm: function (event) {
this.textAreaElement = this.$el.find('[data-tid="announcement"]');
this.messageModel = new MessageModel();
this.messageModel.save({
message: this.textAreaElement.val()
}, {
wait: true,
success: this.onSuccess,
error: this.onError
});
},
onSuccess: function() {
console.log(this.messageModel.get('id')); // undefined
this.collection.add(this.messageModel); // I need to get also the id of the following model
// which is created by the server
}
});
我沒有更新我的問題,把選項'等待:true'。你能檢查它嗎?謝謝。 Unforunatilly當我在onSuccess函數中獲取id時,id是未定義的。它應該是服務器問題嗎? – js999 2012-07-26 07:52:24
是的,服務器需要發送你想要的模型的ID和任何其他值 – 2012-07-26 11:00:36