如何解除绑定 Backbone Collection 获取回调?

发布于 2025-01-03 15:03:16 字数 671 浏览 4 评论 0原文

如果我有一个视图,其中包含在初始化函数中获取的集合,但在获取返回之前需要清理该视图,如何取消绑定成功和/或错误回调?

因此,使用这样的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花开雨落又逢春i 2025-01-10 15:03:16

您始终可以向 this.successCallback 和 this.errorCallback 添加一个逻辑标志来检查 this.close 是否已被调用:

Backbone.View.extend({
  initialize: function () {
    this.collection = new MyColl();
    this.collection.fetch({
      success: this.successCallback,
      error: this.errorCallback
    });
  },
  close: function () {
     // do stuff
     this.closed = true;
  },
  successCallback: function() {
    if(this.closed) return;
    //Do stuff
  }
});

或者,您不应该真正以这种方式设置事件。如果你做类似的事情,它会更加“骨干”:

Backbone.View.extend({
  initialize: function () {
    this.collection = new MyColl();
    this.collection.bind('reset', this.SuccessCallback);
    this.collection.bind('error', this.errorCallback);
  },
  close: function () {
     // do stuff
     this.collection.unbind('reset', this.successCallback);
     this.collection.unbind('error', this.errorCallback);
  },
  successCallback: function() {
    //Do stuff
  }
});

You could always just add a logical flag to this.successCallback and this.errorCallback that checks if this.close has been called:

Backbone.View.extend({
  initialize: function () {
    this.collection = new MyColl();
    this.collection.fetch({
      success: this.successCallback,
      error: this.errorCallback
    });
  },
  close: function () {
     // do stuff
     this.closed = true;
  },
  successCallback: function() {
    if(this.closed) return;
    //Do stuff
  }
});

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:

Backbone.View.extend({
  initialize: function () {
    this.collection = new MyColl();
    this.collection.bind('reset', this.SuccessCallback);
    this.collection.bind('error', this.errorCallback);
  },
  close: function () {
     // do stuff
     this.collection.unbind('reset', this.successCallback);
     this.collection.unbind('error', this.errorCallback);
  },
  successCallback: function() {
    //Do stuff
  }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文