I work a lot in the Node.js server side JavaScript environment and one thing I really like about it is the Events.once() function used to register an event listener, but automatically remove it after the event fires. jQuery offers the same functionality with the .one() function.

This feature is really handy in an asynchronous environment and I found myself missing it in Backbone.js. Since most parts of a Backbone.js app communicate via events, the ability to add and remove event listeners with one call seems like a good thing.

Update: A one time event handler has been added to Backbone.js! Events.once was added with this commit back in Dec 2012, a month after this post was written. The code snippet that follows is no longer needed.

You can do this with Underscore.once() but I think it's difficult for someone reading the code to see what is happening.

So I've started adding this snippet to extend Backbone.js in the apps that I'm working on and so far I've found myself using it quite a lot. One particular use case is preventing side effects from a user clicking a button multiple times.

Comment on this Backbone.js issue tracker thread to help push this feature into Backbone.js. So far the community does not see an overwhelming need for it, but I've found it invaluable. Update: A one time event handler has been added to Backbone.js!