Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backbone.Events.once #594

Closed
wants to merge 1 commit into from
Closed

Backbone.Events.once #594

wants to merge 1 commit into from

Conversation

m64253
Copy link

@m64253 m64253 commented Sep 3, 2011

Simple event helper to bind to an event an only have the callback trigger once.

@niallsmart
Copy link
Contributor

Why not just use _.once? For example:

model.bind(event, _.once(callback));

@yuchi
Copy link

yuchi commented Sep 27, 2011

'Cause otherwise it will be called every time anyway. It's useless and wasting. Unbinding permits garbage collecting every reference.

@jashkenas
Copy link
Owner

To repeat myself from earlier this afternoon: I don't think that this is a common enough need to warrant addition to Backbone.Events ... especially when _.once is guaranteed to be available.

@jashkenas jashkenas closed this Jan 13, 2012
@khepin
Copy link

khepin commented Aug 10, 2012

Just to put my grain of salt in there, I definitely see value in having such a "once" method, and _.once does not help in this case.

_.once returns the same result without calling the function again. But in some case the return value of a function is not what's interesting.

Sometimes it's an api call, sometimes it's some DOM manipulation. If I need to re-bind it later, then using _.once completely defeats the purpose.

@tedsuo
Copy link

tedsuo commented Aug 19, 2012

Just came across this, thought I'd add another +1. For me, it's pretty common to only bind once in two cases: events that only happen once ( like an 'end' event), and events that happen frequently but are very decoupled from what is being hung on them ( a 'nextTick' event is an example). In both cases, it's important that references be cleaned up to prevent memory leaks. Mostly I do this in node.js, somewhat new to backbone but hit the same issue almost immediately.

Cheers,
T

@iturgeon
Copy link

iturgeon commented Sep 2, 2012

fwiw, I could use this functionality right now

@philfreo
Copy link
Contributor

philfreo commented Sep 2, 2012

Yesterday I also needed something like http://api.jquery.com/one/ (and _.once isn't the same)

tbranyen added a commit to tbranyen/backbone that referenced this pull request Sep 13, 2012
@monokrome
Copy link

I'm interested in this, also.

Although using _.once does allow for a similar result - it does not unbind the event handler. If you have a long running application with many events, this would still result in many potentially unnecessary calls to once() simply because the method wasn't unbound.

This can be fixed by calling Event.off in each of your handlers that need to be called only once, but you may not have access to the context in some cases where you've provided a this argument. Using Event.off actually gets the intended result when you have the proper context to the object which on was called on, but my opinion is that it shouldn't be necessary to call off inside of your Event handler.

For instance, if I wanted to use jQuery.noop (as an example) as my handler, I would now have to do the following because I don't have access to the context of MyObject inside of noop:

// I can't use a lambda here, or I wouldn't be able to reference it from itself.
var something = function () {
    myInstance.off('exploded', something);
    jQuery.noop();
}

myInstance.on('exploded', something);

A much more simple solution would be:

myInstance.once('exploded', jQuery.noop);

I would think that wanting an event to only fire a specific handler a single time isn't too rare of a request, and it's definitely something that should be taken account in Backbone.

@khepin khepin mentioned this pull request Oct 27, 2012
tbranyen added a commit to tbranyen/backbone that referenced this pull request Dec 4, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants