Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix region clean up, when view destroy #3262

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/region.js
Expand Up @@ -72,6 +72,10 @@ const Region = MarionetteObject.extend({

this._attachView(view, options);

if (this._isReplaced) {
view.on('before:destroy', this._empty, this);
Copy link
Member

@rafde rafde Nov 17, 2016

Choose a reason for hiding this comment

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

}

this.triggerMethod('show', this, view, options);
return this;
},
Expand Down Expand Up @@ -225,6 +229,10 @@ const Region = MarionetteObject.extend({
},

_empty(view, shouldDestroy) {
if (this._isReplaced) {
view.off('before:destroy', this._empty, this);
}

view.off('destroy', this._empty, this);
this.triggerMethod('before:empty', this, view);

Expand Down
16 changes: 16 additions & 0 deletions test/unit/region.spec.js
Expand Up @@ -337,6 +337,22 @@ describe('region', function() {
expect(this.$parentEl).to.contain.$html('<div id="region"></div>');
});
});

describe('and restore, when view was self destroyed', function() {
beforeEach(function() {
this.view.trigger('before:destroy', this.view);
this.view._removeElement();
Copy link
Member

Choose a reason for hiding this comment

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

I think this.view.destroy(); should do the event emitting instead in order to match a real scenario.

this.view.trigger('destroy', this.view);
});

it('should remove the view from the parent', function() {
expect(this.$parentEl).to.not.contain.$html(this.view.$el.html());
});

it('should restore the region\'s "el" to the DOM', function() {
expect(this.$parentEl).to.contain.$html('<div id="region"></div>');
});
});
});

});
Expand Down