Cleanup all subscriptions when a view is removed from the DOM

This commit is contained in:
Corey Johnson & Nathan Sobo
2012-12-14 14:29:20 -08:00
parent 085a5ea54f
commit 528fae9466
2 changed files with 8 additions and 3 deletions

View File

@@ -32,10 +32,9 @@ describe "SpacePen extensions", ->
expect(observeHandler).not.toHaveBeenCalled()
it "unsubscribes on remove", ->
it "unsubscribes when the view is removed from the DOM", ->
observeHandler.reset()
view.remove()
expect(view.hasParent()).toBeFalsy()
parent.remove()
config.foo = { bar: "hello" }
config.update()
expect(observeHandler).not.toHaveBeenCalled()

View File

@@ -1,5 +1,6 @@
_ = require 'underscore'
{View} = require 'space-pen'
jQuery = require 'jquery'
originalRemove = View.prototype.remove
@@ -17,3 +18,8 @@ _.extend View.prototype,
remove: (args...) ->
@unsubscribe()
originalRemove.apply(this, args)
originalCleanData = jQuery.cleanData
jQuery.cleanData = (elements) ->
jQuery(element).view()?.unsubscribe?() for element in elements
originalCleanData(elements)