From a986fddd5df92acc67ed9f3e82fa859d211bb9fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luiz=20Am=C3=A9rico?= Date: Sun, 26 Nov 2017 09:26:18 -0300 Subject: [PATCH] Implement setRenderer destructive option: when false, do not reinit regions --- src/config/renderer.js | 5 ++++- src/view.js | 2 +- test/unit/backbone.marionette.spec.js | 19 +++++++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/config/renderer.js b/src/config/renderer.js index 43bb2574c8..864aa28eff 100644 --- a/src/config/renderer.js +++ b/src/config/renderer.js @@ -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) { + this.prototype._preventRegionReInit = true; + } return this; } diff --git a/src/view.js b/src/view.js index e5ca4ef8b0..0ea2e7038f 100644 --- a/src/view.js +++ b/src/view.js @@ -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(); } diff --git a/test/unit/backbone.marionette.spec.js b/test/unit/backbone.marionette.spec.js index 7ecb79d763..f1ae8d80d8 100644 --- a/test/unit/backbone.marionette.spec.js +++ b/test/unit/backbone.marionette.spec.js @@ -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; + }); + }); }); });