Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Shouldn't `listenTo` also `bind` the listener to the context? #1999

Closed
fabiomcosta opened this Issue · 5 comments

2 participants

@fabiomcosta

As you can see, this is more of a question than an issue.

Shouldn't listenTo also bind the listener to the context?

I see myself (probably others are doing the same) doing this kind of thing inside my views:

...
        initialize: function() {
            _.bindAll(this, 'onReset', 'onAdd');

            this.listenTo(this.collection, 'add', this.onAdd);
            this.listenTo(this.collection, 'reset', this.onReset);
        },
...

If we bind the listener to the instance, we could save the _.bindAll and write just:

...
        initialize: function() {
            this.listenTo(this.collection, 'add', this.onAdd);
            this.listenTo(this.collection, 'reset', this.onReset);
        },
...
@caseywebdev
Collaborator

It does. Your top and bottom code will act identically.

@fabiomcosta

No it doesn't, this inside this.onAdd and this.Reload is the collection instead of the view.

https://github.com/documentcloud/backbone/blob/master/backbone.js#L193-L200

@caseywebdev
Collaborator

Does this clear it up?

http://jsfiddle.net/WWguP/

@fabiomcosta

Looks like I'm wrong again!
That's great news!
Thank you!

@caseywebdev
Collaborator

No problem again!

@recurse recurse referenced this issue from a commit in recurse/backbone
@recurse recurse Document listenTo's implicit 'this' context.
That the context to the callback in listenTo is this, is only
documented as issue discussions on github. This adds explicit
documentation of that semantic.

See Issue #1999
9b9046e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.