Skip to content

Commit

Permalink
Make sure $el contains on the el
Browse files Browse the repository at this point in the history
Alternative solution to #3421

I think doing this check once on `_ensureElement` is better than within `attachHtml`  And it makes `this.$el` more consistent and matches what happens in the region constructor
  • Loading branch information
paulfalgout authored and JSteunou committed Aug 17, 2017
1 parent 5e6d5eb commit 575bf47
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 20 deletions.
4 changes: 3 additions & 1 deletion src/region.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ const Region = MarionetteObject.extend({
if (!_.isObject(this.el)) {
this.$el = this.getEl(this.el);
this.el = this.$el[0];
// Make sure the $el contains only the el
this.$el = this.Dom.getEl(this.el);
}

if (!this.$el || this.$el.length === 0) {
Expand Down Expand Up @@ -241,7 +243,7 @@ const Region = MarionetteObject.extend({
// Override this method to change how the new view is appended to the `$el` that the
// region is managing
attachHtml(view) {
this.Dom.appendContents(this.el, view.el, {_$contents: view.$el});
this.Dom.appendContents(this.el, view.el, {_$el: this.$el, _$contents: view.$el});
},

// Destroy the current view, if there is one. If there is no current view, it does
Expand Down
55 changes: 36 additions & 19 deletions test/unit/common/build-region.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,29 +115,46 @@ describe('Region', function() {
expect(this.region.el).to.equal(this.fooSelector);
});

describe('with `parentEl` also defined including the selector', function() {
beforeEach(function() {
this.setFixtures('<div id="parent"><div id="child">text</div></div>');
this.parentEl = $('#parent');
this.definition = _.defaults({parentEl: this.parentEl, el: '#child' }, this.definition);
this.region = this.view.addRegion(_.uniqueId('region_'),this.definition);
});

it('returns the jQuery(el)', function() {
expect(this.region.getEl(this.region.el).text()).to.equal($(this.region.el).text());
describe('with `parentEl` also defined', function() {
describe('including the selector', function() {
beforeEach(function() {
this.setFixtures('<div id="parent"><div id="child">text</div></div>');
this.parentEl = $('#parent');
this.definition = _.defaults({parentEl: this.parentEl, el: '#child' }, this.definition);
this.region = this.view.addRegion(_.uniqueId('region_'),this.definition);
});

it('returns the jQuery(el)', function() {
expect(this.region.getEl(this.region.el).text()).to.equal($(this.region.el).text());
});
});
});

describe('with `parentEl` also defined excluding the selector', function() {
beforeEach(function() {
this.setFixtures('<div id="parent"></div><div id="not-child">text</div>');
this.parentEl = $('#parent');
this.definition = _.defaults({parentEl: this.parentEl, el: '#not-child' }, this.definition);
this.region = this.view.addRegion(_.uniqueId('region_'),this.definition);
describe('excluding the selector', function() {
beforeEach(function() {
this.setFixtures('<div id="parent"></div><div id="not-child">text</div>');
this.parentEl = $('#parent');
this.definition = _.defaults({parentEl: this.parentEl, el: '#not-child' }, this.definition);
this.region = this.view.addRegion(_.uniqueId('region_'),this.definition);
});

it('returns the jQuery(el)', function() {
expect(this.region.getEl(this.region.el).text()).to.not.equal($(this.region.el).text());
});
});

it('returns the jQuery(el)', function() {
expect(this.region.getEl(this.region.el).text()).to.not.equal($(this.region.el).text());
describe('including multiple instances of the selector', function() {
beforeEach(function() {
this.setFixtures('<div id="parent"><div class="child">text</div><div class="child">text</div></div>');
this.parentEl = $('#parent');
this.definition = _.defaults({parentEl: this.parentEl, el: '.child' }, this.definition);
this.region = this.view.addRegion(_.uniqueId('region_'),this.definition);
});

it('should ensure a jQuery(el) of length 1', function() {
// calls _ensureElement
this.region.empty();
expect(this.region.$el.length).to.equal(1);
});
});
});
});
Expand Down

0 comments on commit 575bf47

Please sign in to comment.