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

Implement setRenderer destructive option #3556

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/config/renderer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// Static setter for the renderer
export function setRenderer(renderer) {
export function setRenderer(renderer, options) {
this.prototype._renderHtml = renderer;
if (options && options.destructive === false) {
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 the feature could be good, though I don't like the name.

Maybe just call this reInitializeRegion or maybe preserveRegion so the flag check can be options.preserveRegion.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm fine changing the name but should not be region specific. This flag may be needed for other things than reinitialize region like for example adding an onReady event which would have a different behavior from destructive and non destructive regions.

Copy link
Member

Choose a reason for hiding this comment

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

What about preventDestroy? It's used in stuff like region.show and it feels like it does a similar job.

this.prototype._preventRegionReInit = true;
}
return this;
}
2 changes: 1 addition & 1 deletion src/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ const View = Backbone.View.extend({

// If this is not the first render call, then we need to
// re-initialize the `el` for each region
if (this._isRendered) {
if (this._isRendered && !this._preventRegionReInit) {
this._reInitRegions();
}

Expand Down
19 changes: 19 additions & 0 deletions test/unit/backbone.marionette.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,24 @@ describe('backbone.marionette', function() {
expect(Class.setRenderer).to.be.calledOnce.and.calledWith(fakeRenderer);
});
});

describe('when destructive option is set to false', function() {
const TestView = View.extend({
template: _.noop
});
let view;

beforeEach(function() {
TestView.setRenderer(renderer, {destructive: false});
view = new TestView();
view.render();
this.sinon.spy(view, '_reInitRegions');
view.render();
});

it('should not re-init regions on view re-render', function() {
expect(view._reInitRegions).to.not.be.called;
});
});
});
});