Skip to content
This repository has been archived by the owner on Jun 8, 2019. It is now read-only.

Added Event Cleanup Helpers #5

Closed
wants to merge 16 commits into from
Closed

Conversation

amkirwan
Copy link
Contributor

Added some helpers for event cleanup when the view is removed. These are taken from the book and put into a mixin Support.Observer that is mixed into the Support.CompositeView. I have also added a callback function onLeave which will be called from the view if the user has implemented it before the view is removed from the DOM.

@@ -5,6 +5,10 @@ Support.CompositeView = function(options) {

_.extend(Support.CompositeView.prototype, Backbone.View.prototype, {
leave: function() {
if (this.onLeave) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a Template Method like this doesn't feel natural in the Backbone world of events. I think having a "leave" event to bind to would be more natural.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good. I'll make the change.

title: 'Model or Collection'
});

it("calls the unbindFromAll method when leaving the view", function() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't actually test that it ever binds to the event, does it? If you comment out line 3 of observer.js, do all the specs pass? This applies to the unbindFromAll spec below, as well as the specs for Observer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you comment out line 3 of observer.js the observer_spec.js will fail because the test on line 45 checks if bind is called on the source object. The #unbindFromAll test will pass though because I don't test if bind is called. My thoughts were that this was something more in the domain of the observer_spec than the composite_view_spec but I can add a test to #unbindFromAll to check that bind is called on the source object if you think it needs one.

@jferris
Copy link
Contributor

jferris commented Jul 20, 2012

Thanks for putting this together. This is a good change overall, but I had a few comments on the diff. I'll go over this patch again once you've had a chance to look those over.

@amkirwan
Copy link
Contributor Author

I made a few changes to the code per your suggestions and added them to the branch.

@jferris
Copy link
Contributor

jferris commented Jul 23, 2012

@amkirwan looks good. I'd like to rename onLeave to just leave to be more in line with existing Backbone events (change, error, etc). You can do that if you want, or I'll do it myself when I merge the branch in a bit. Thanks again for the patch!

@amkirwan
Copy link
Contributor Author

@jferris I agree the event makes more sense as 'leave' than 'onLeave' for Backbone. Thanks for your help.

@lokimeyburg
Copy link

I'm really liking these helpers! I'm using them right now.
Let me know if you need any help.
I'd like to see this merged.

@@ -164,4 +200,80 @@ describe("Support.CompositeView", function() {
expect(view.children.size()).toEqual(1);
});
});

/*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noticed that this spec is commented out. Should it be removed or uncommented?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my bad this needs to be removed. That was from before we switched to using a leave event.

@jferris
Copy link
Contributor

jferris commented Nov 9, 2012

Sorry for the delay on this. I went through the pull request again and had a few more comments.

@amkirwan
Copy link
Contributor Author

@jferris let me know if you need anything else to merge the changes

@jferris
Copy link
Contributor

jferris commented Nov 21, 2012

Thanks. I merged this into master.

@jferris jferris closed this Nov 21, 2012
@lokimeyburg
Copy link

👍

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants