marionettejs/backbone.marionette — pull request #2171 by keithstric (2014-12-30 20:07:28+00:00)
Title: Adds the childViews property to a behavior. When the behavior's "view" i...

1. I have been using behaviors to add buttons and various functionality that is needed throughout my app. I wanted to create one behavior say for a filter feature that is used in many different places. This 'filter' button of course would require a view and corresponding template be created. However, when the behavior's view was destroyed I was seeing zombie views surfacing for the views I had created in the behavior. This pull request is meant to address this issue.

2. I added a property to the behavior class called childViews. This is an array placeholder to place any views created by the behavior. Then in the destroy method, call destroy on each of the views in the childViews array.

3. When the behavior's parent view is destroyed, it calls the destroy method in the behavior. I added functionality that will call the destroy method on each of the views in the childViews array. Also, in the behavior.spec.js file, there was a test there for destroy that stated destroy should return the behavior. This was not being checked, so I also added a 'return this;' line to the destroy method and fixed the unit test to actually verify that the destroy method returns the behavior. I also added another unit test to ensure that destroy was being called on the views in the childViews property.

4. Since adding the 'return this;' line to the destroy method of a behavior, I couldn't determine if this will have any unwanted side effects. From my initial testing (in my app) it doesn't appear that it does but I of course could be wrong. Also, I'm sure there is a better way to keep track of the views that a behavior may/may not create, it would be great if this were a little more elegant but I'm not sure how I could accomplish that.