bindOnce - only fire bind callback once #663

Closed
wants to merge 1 commit into
from

Conversation

Projects
None yet
5 participants
@studgeek

studgeek commented Oct 8, 2011

Added bindOnce with tests - bind an event like bind(), but unbind the callback after the first time its called.
Similar to jQuery one and Node.js emitter.once. See issue #156 for requests, discussion, and alternative implementation.

Added bindOnce with tests - bind an event like bind(), but unbind the…
… callback after the first time its called.

Similar to jQuery one and Node.js emitter.once. See issue #156 for requests, discussion, and alternative implementation.
@gilesbowkett

This comment has been minimized.

Show comment
Hide comment
@gilesbowkett

gilesbowkett Oct 8, 2011

This looks identical to #309

This looks identical to #309

@studgeek

This comment has been minimized.

Show comment
Hide comment
@studgeek

studgeek Oct 8, 2011

It doesn't seem that way to me unless I am missing something.

#309 fixes a problem with unbinding during a callback, regardless of how you do it.

This explicitly adds a bindOnce function (and test) that unbinds after the first callback. So this depends on #309, but I wouldn't say its the same.

studgeek commented Oct 8, 2011

It doesn't seem that way to me unless I am missing something.

#309 fixes a problem with unbinding during a callback, regardless of how you do it.

This explicitly adds a bindOnce function (and test) that unbinds after the first callback. So this depends on #309, but I wouldn't say its the same.

@wookiehangover

This comment has been minimized.

Show comment
Hide comment
@wookiehangover

wookiehangover Oct 29, 2011

Collaborator

+1

Collaborator

wookiehangover commented Oct 29, 2011

+1

@wookiehangover

This comment has been minimized.

Show comment
Hide comment
@wookiehangover

wookiehangover Oct 29, 2011

this2 seems a bit odd... maybe self is a better naming choice?

this2 seems a bit odd... maybe self is a better naming choice?

This comment has been minimized.

Show comment
Hide comment
@studgeek

studgeek Oct 29, 2011

Owner

That does seem to the more common name, I personally have never liked it because it so different from "this" and because it reminds me how much I miss Smalltalk :).
All that said, the right choice choice is to go with the flow and even better would be consistency with existing backbone.js code. And that seems to be using _.bind. So I will tweak it over to that (even though it may be a bit confusing in this context) and update the branch.
Thanks for suggestion, d.
PS, Why oh why did JS do this to us? IMHO, the global binding of this is the worst aspect of JS language - and it has a lot of them :).

Owner

studgeek replied Oct 29, 2011

That does seem to the more common name, I personally have never liked it because it so different from "this" and because it reminds me how much I miss Smalltalk :).
All that said, the right choice choice is to go with the flow and even better would be consistency with existing backbone.js code. And that seems to be using _.bind. So I will tweak it over to that (even though it may be a bit confusing in this context) and update the branch.
Thanks for suggestion, d.
PS, Why oh why did JS do this to us? IMHO, the global binding of this is the worst aspect of JS language - and it has a lot of them :).

This comment has been minimized.

Show comment
Hide comment
@taybin

taybin Nov 2, 2011

I believe
var that = this;
is the idiom you're looking for, according to JavaScript: The Good Parts.

I believe
var that = this;
is the idiom you're looking for, according to JavaScript: The Good Parts.

This comment has been minimized.

Show comment
Hide comment
@studgeek

studgeek Nov 2, 2011

Owner

Yes, that one is popular also, but I like it even less because it implies its something else. I like my this2 because (in my head at least) I can think of it as "this also". - but Crockford has more weight on this than I do :). In any case, for backbone I will swap this to _.bind to be consistent with other backbone code.

Owner

studgeek replied Nov 2, 2011

Yes, that one is popular also, but I like it even less because it implies its something else. I like my this2 because (in my head at least) I can think of it as "this also". - but Crockford has more weight on this than I do :). In any case, for backbone I will swap this to _.bind to be consistent with other backbone code.

@buger

This comment has been minimized.

Show comment
Hide comment
@buger

buger Oct 29, 2011

Underscore already have method "once".

this.model.bind('chage:title', _.once(this.callback)); // This will call callback only one time

buger commented Oct 29, 2011

Underscore already have method "once".

this.model.bind('chage:title', _.once(this.callback)); // This will call callback only one time
@wookiehangover

This comment has been minimized.

Show comment
Hide comment
@wookiehangover

wookiehangover Oct 29, 2011

Collaborator

@buger good point, no reason to have code duplication between underscore and backbone

Collaborator

wookiehangover commented Oct 29, 2011

@buger good point, no reason to have code duplication between underscore and backbone

@studgeek

This comment has been minimized.

Show comment
Hide comment
@studgeek

studgeek Nov 16, 2011

Cool! Closing (since pull request was actually included and commited by @tbranyen in 21abc4a).

Cool! Closing (since pull request was actually included and commited by @tbranyen in 21abc4a).

@studgeek studgeek closed this Nov 16, 2011

tbranyen added a commit to tbranyen/backbone that referenced this pull request Sep 13, 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