最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Use off() directly in on() callback Firebase? - Stack Overflow

programmeradmin0浏览0评论

Let's say I have a one and done operation. Like deletes.

var q = ref.child('users').child(targetUserId).child('chat');
q.on('child_added', function(obj) {
  obj.ref().remove();

  //This works right?
  q.off();
}); 

Can I execute off() directly in the on() callback? I don't need to specify eventType correct?

Nothing else to clean up right?

From the docs:
Similarly, if no eventType or callback is specified, all callbacks for the reference will be removed.

Let's say I have a one and done operation. Like deletes.

var q = ref.child('users').child(targetUserId).child('chat');
q.on('child_added', function(obj) {
  obj.ref().remove();

  //This works right?
  q.off();
}); 

Can I execute off() directly in the on() callback? I don't need to specify eventType correct?

Nothing else to clean up right?

From the docs:
Similarly, if no eventType or callback is specified, all callbacks for the reference will be removed.

Share Improve this question asked Feb 1, 2015 at 18:57 Dan KanzeDan Kanze 18.6k28 gold badges84 silver badges135 bronze badges 3
  • 4 If you're turning it on and off again like that, you may consider using once instead. – Frank van Puffelen Commented Feb 1, 2015 at 19:29
  • @FrankvanPuffelen I was trying to use once() on a slightly different version of the example above - q.endAt(estimatedServerTimeMs-5000).once(...) - but the remove() would only delete a single record (the first one in the matching set). Changing once to on would delete all of the matching records. Any idea why that might be? – Dan Kanze Commented Feb 1, 2015 at 20:12
  • 2 When using once Firebase will only fire a single event. So in case of child_added it will only fire for a single child (no matter how many children match the query). For your original question that is precisely what you need. But indeed for the use-case in your ment, stick to on('child_added' and off it on the right condition or alternatively once('value' and then forEach over the children in the snapshot. – Frank van Puffelen Commented Feb 1, 2015 at 20:55
Add a ment  | 

1 Answer 1

Reset to default 5

Can I execute off() directly in the on() callback?

Yes.

I don't need to specify eventType correct?

Correct. As the docs state, if the eventType is omitted, all callbacks for the reference will be removed.

You can also just chain the .off() method like this rather than calling it inside of the callback:

var q = ref.child('users').child(targetUserId).child('chat');
q.on('child_added', function(obj) {
  obj.ref().remove();
}).off();

As Frank van Puffelen suggests in the ments, you can also use the .once() method in order for the callback to only be executed once:

var q = ref.child('users').child(targetUserId).child('chat');
q.once('child_added', function(obj) {
  obj.ref().remove();
});

The benefit to this approach is that you don't have to worry about inadvertently removing other attached events.

发布评论

评论列表(0)

  1. 暂无评论