backbone.jsを使用してフォームを送信しようとしています。この場合、this
は使用できません。だから私は引数を使ってフォームの内容をjsonにシリアル化することを望んでいます。Backbone.jsイベントを.createからフォームのシリアル化
どうすればよいですか?
window.UserView = Backbone.View.extend({
el: 'body',
model: new window.UserList,
template: _.template($('#user-view-template').html()),
events: { "submit form#newUser": "addItem" },
initialize: function() {
_.bindAll(this, 'render', 'update', 'addItem');
this.model.bind('refresh', this.render);
this.update();
},
render: function() {
$("#listContainer").html(this.template({ users: this.model.toJSON() }));
return this;
},
update: function() {
this.model.fetch();
},
addItem: function (e) {
e.preventDefault();
var person_attrs = $(e.target).serializeObject();
this.model.create(person_attrs, {
success: function (saved_person, data) {
if (data.Result == "Success") {
alert("success, new Id: " + data.Model.Id);
}
else {
//Need to remove the model from the collection
alert(data.Message);
}
},
error: function (aborted_person, response) {
alert("Server Error");
}
});
return false;
}
});
これまでのところ、シリアル化してサーバーに送信するフォームがありました。これは正しいことですか?
私はまた、他のいくつかの問題を抱えている:
- 地元の作成モデルを削除するか、キャンセルサーバーがエラーを返した場合。
- サーバの結果が「成功」の場合、返されたIDをモデルに割り当てたいと思います。
- ビューを更新します。私は、サーバが返された後にイベント
refresh
が呼び出されると思った。
DOM操作のためにZeptoまたはjQueryを使用していますか(参考にする必要がありますか?) – Shakakai
Shakaai - Jquery –
質問を長すぎたり複雑にしたりすると、回答が落ちる傾向があります。短く集中しているほうがずっと良いです。あなたのid属性の世話をするモデルの解析メソッドについて読む必要があります。ビューメソッドをモデルのchangeイベントにバインドしないかぎり、ビューは更新されません。ドキュメンテーションでは、イベント・バインディングについて読む必要があります。 –