如何解除绑定 Backbone Collection 获取回调?
如果我有一个视图,其中包含在初始化函数中获取的集合,但在获取返回之前需要清理该视图,如何取消绑定成功和/或错误回调?
因此,使用这样的代码:
Backbone.View.extend({
initialize: function () {
this.collection = new MyColl();
this.collection.fetch({
success: this.successCallback,
error: this.errorCallback
});
},
close: function () {
// what goes here to keep successCallback and errorCallback from being called?
}
});
当我调用 myView.close() 来清理它(在本例中显示另一个视图)时,我不想稍后调用 successCallback (在视图“cleanup”之后)。
我已经尝试过:
close: function () {
this.collection.unbind('reset');
}
但是该集合在获取后似乎没有在其 _callbacks 内部变量中列出此事件,因此取消绑定似乎没有帮助。
If I have a view that has a collection that is fetched in the initialize function, but the view needs cleaned up before the fetch returns, how do I unbind the success and/or error callbacks?
So, with code like this:
Backbone.View.extend({
initialize: function () {
this.collection = new MyColl();
this.collection.fetch({
success: this.successCallback,
error: this.errorCallback
});
},
close: function () {
// what goes here to keep successCallback and errorCallback from being called?
}
});
When I call myView.close() to clean it up (in this case to show another view), and I don't want to be calling successCallback later (after view "cleanup").
I've tried:
close: function () {
this.collection.unbind('reset');
}
But the collection doesn't seem to have a this event listed in its _callbacks internal var after fetch, so unbind doesn't seem to help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您始终可以向 this.successCallback 和 this.errorCallback 添加一个逻辑标志来检查 this.close 是否已被调用:
或者,您不应该真正以这种方式设置事件。如果你做类似的事情,它会更加“骨干”:
You could always just add a logical flag to this.successCallback and this.errorCallback that checks if this.close has been called:
Alternatively, you should not really be setting up your events in this manner. It is much more "backbone-ish" if you instead do something similar to: