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

Added "one" method to all classes inheriting from Backbone.Event. #2

Merged
merged 3 commits into from
Dec 22, 2011
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions lib/appetizer/ui/app/js/appetizer/core.coffee
Original file line number Diff line number Diff line change
@@ -1 +1,20 @@
window.Appetizer ||= {}

# All Backbone's classes that inherit from Backbone.Event
# are also given a "one" method, which can be used to fire
# a callback only on the next occurence of a given event.

one =
one: (ev, callback, context) ->
self = this

fn = ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason all the cool kids use .apply, but in this case I think it's the same as this which is much easier to read:

this.unbind ev, fn

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue is that, when fn is executed, its context may have changed. For instance, in jQuery, the callback is executed within the calling element's context. Thus, by using apply here, we make sure that we call unbind and bind with the correct context..

callback.apply this, arguments
self.unbind.apply self, [ev, fn]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, right? Feel free to leave it with the apply. your call.

this.bind.apply this, [ev, fn, context]

_.extend Backbone.Model.prototype, one
_.extend Backbone.Collection.prototype, one
_.extend Backbone.View.prototype, one
_.extend Backbone.Router.prototype, one