marionettejs/backbone.marionette — pull request #379 by vhalis (2012-12-12 10:46:55+00:00)
Title: Changed Marionette.View to handle entity events alongside normal events ...

(delegating and undelegating of entity events)

Sorry if I've missed anything, this is my first pull request. :) Let me know what I can do. I branched from master but I don't think it's different than dev at the moment.. sorry.. I'm not sure how to fix it. I'm not sure how to create a spec, either.

Why:
While using a setElement call on a view to a region then destroying/recreating the layout with that region, I ran into an issue with none of the model-based events firing. It seems that when the element the view is attached to gets destroyed, all of the entity events are gone, and since they are only called in the constructor, they don't get replaced. Basically, my entity events disappeared after I closed and recreated a layout and put the view with the entity events back into that layout's region.

What I did:
Handled the entity events like more normal events by delegating and undelegating them at usual places for other types of events.

Detail:
I moved the call to bindEntityEvents for a model and collection out of the constructor and in to the delegateEvents function, just after the call to the Backbone version of the function. Since they are events, I wanted to delegate them with the delegation of the other events.

I created an undelegateEvents function since the EventBinder module acts separately from the Backbone events, and the normal Backbone undelegateEvents does not remove any that were created with EventBinder. It first calls the Backbone version. I also created an unbind equivalent of bindEntityEvents, which the new Marionette.View.undelegateEvents uses, that takes the same arguments and can be used on both functions and strings with multiple function specifiers. It's not verbose at the moment, but the functionality is all there.

With delegate and undelegate in these locations, the callbacks were applied only in the amount specified, and the destroying and recreating of layouts and regions didn't remove the callbacks or give them the wrong context.

I can try and create a simple example if you like. Thanks for your work!

- Dan