類似的問題已經在這裏問How do I trigger the success callback on a model.save()?,但仍然沒有答案如何從回調觸發事件。從回調訪問`this`
所以這裏是success
在我的代碼中回調,其中我想調用addOne
事件來渲染保存的Comment。一切運作良好,除了this.addOne(receivedItem);
- 我不能在回調中使用this
來觸發此事件。其他地方 - 我可以。
如何解決這個問題?
CommentsListView = Backbone.View.extend({
...
addOne: function (item) {
var commentView = new CommentView({
model: item
});
this.$el.append(commentView.render().el);
},
addNewComment: function (event) {
var item = {
post_id: this.$('#post_id').val(),
text: this.$('#text').val()
};
var commentItem = new CommentItem();
commentItem.save({'model':item}, {
success: function(receivedItem, response) {
this.addOne(receivedItem); // Uncaught TypeError: Object [object Window] has no method 'addOne'.
}
}, this);
}
});
的可能的複製[如何訪問正確的\'這\'/上下文回調裏面?](http://stackoverflow.com/questions/20279484 /如何訪問這個正確的回調內部) – Bergi