ユーザーが何かをクリックするとビューを削除し、そのビューに関連付けられているモデルも削除しようとしています。モデルを正常に削除しましたが、この.model.destroy メソッドはサーバー リクエストを送信しませんか?
これは私のコードがどのように見えるかです:
PostsApp.Models.Post = Backbone.Model.extend({
url : '/tweet',
defaults:{
name: '',
adress: '',
pictureUrl: '',
postListing: '',
comments: ''
}
});
PostsApp.Collections.Posts = Backbone.Collection.extend({
model: PostsApp.Models.Post,
url: '/tweet'
});
PostsApp.Views.Post = Backbone.View.extend({
template: _.template($('#post-template').html()),
events: {
'click img': 'removeit'
},
removeit: function(){
this.remove();
this.model.destroy();
},
render: function(){
this.$el.html(this.template(this.model.toJSON()));
}
});
サーバー側で Express を使用していますが、サーバー側で HTTP 削除要求を処理するにはどうすればよいですか? しかし、リクエストが送信されていないようですか?
編集:
そのため、モデルを次のように変更して少し進歩しました。
PostsApp.Models.Post = Backbone.Model.extend({
urlRoot : '/tweet',
idAttribute: '_id',
defaults:{
name: '',
adress: '',
pictureUrl: '',
postListing: '',
comments: ''
},
kill: function(){
this.destroy({success: function(model, response) {
console.log("sucess");
}});
}
});
サーバー側のルーターは、リクエストを処理するために次のように設定されています。
app.delete('/tweet/:id'), function(req,res){
};
現在、削除リクエストは次のように送信されています: DELETE /tweet/51b2548ba8568587ea000002 しかし、次のような 404 エラーが発生しています: DELETE localhost:3000/tweet/51b2548ba8568587ea000002 404 (Not Found)