Skip to content

Instantly share code, notes, and snippets.

@richsoni
Created August 26, 2013 17:11
Show Gist options
  • Save richsoni/6343963 to your computer and use it in GitHub Desktop.
Save richsoni/6343963 to your computer and use it in GitHub Desktop.
Put your backbone zombies through the woodchipper
#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
# 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)
# 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()
# 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
# 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