Created
August 26, 2013 17:11
-
-
Save richsoni/6343963 to your computer and use it in GitHub Desktop.
Put your backbone zombies through the woodchipper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#keep tabs on your vent bindings. If you dont kill them they will slowly walk toward your app, and suck its brains (memory) out | |
################# | |
# INSTEAD OF | |
# App.Vent.bind('some_event', _.bind(some_callback, this)) | |
# App.Vent.bind('other_event', _.bind(other_callback, this)) | |
# Do | |
@bindGlobalEvents | |
some_event: 'someCallback' | |
other_event |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Behind the scenes this will create a global array of pointers to your new Vent objects | |
# It also will bind these events to @ for you | |
bindGlobalEvents: (events) -> | |
@globalEvents ||= [] | |
for vent, callback of events | |
fun = _.bind @[callback], @ | |
@globalEvents.push App.Vent.bind(vent, fun) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# use a global variable to hold the view, and close that sucker before making a new one. | |
# if you dont it will come back and suck your brains | |
myView.close if myView | |
myView = new Backbone.View() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Behind the scenes close will kill all those pesky global bindings and anything else needed to axe this sucker | |
close: -> | |
vent.unbind() for vent in @globalEvents || [] | |
@remove() | |
@unbind() | |
# write custom cleanup in your view | |
@onClose() if @onClose |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Add an onClose method to do some callback action after the view is under flowers | |
onClose: -> @walkSafeAtNight() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment