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

WIP: Add auto discover of region element #3530

Closed
wants to merge 2 commits 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
68 changes: 64 additions & 4 deletions src/mixins/regions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import _ from 'underscore';
import _invoke from '../utils/invoke';
import buildRegion from '../common/build-region';
import MarionetteError from '../error';
import Region from '../region';

// MixinOptions
Expand Down Expand Up @@ -126,11 +127,16 @@ export default {
// Provides access to regions
// Accepts the region name
// getRegion('main')
getRegion(name) {
getRegion(name, options) {
if (!this._isRendered) {
this.render();
}
return this._regions[name];
const region = this._regions[name];
if (region && region.el) {
return region;
} else {
return this.autoAddRegion(region, name, options);
}
},

// Get all regions
Expand All @@ -146,7 +152,7 @@ export default {
},

showChildView(name, view, ...args) {
const region = this.getRegion(name);
const region = this.getRegion(name, ...args);
region.show(view, ...args);
return view;
},
Expand All @@ -157,6 +163,60 @@ export default {

getChildView(name) {
return this.getRegion(name).currentView;
}
},

// allow the user to provide its own selector
getRegionsSelector() {
return '[data-region]';
},

// allow the user to provide its own selector
getRegionSelector(name) {
return `[data-region="${name}"]`;
},

findRegionElement(name) {
return this.Dom.findEl(`[data-view-cid="${this.cid}"]${this.getRegionSelector(name)}`);
},

// allow the user to add more
// default options from el data attribute
getRegionDefaultOption(name, el) {
return {el};
},

idRegionElements() {
const regionEls = this.Dom.findEl(this.el, this.getRegionSelector());
if (regionEls.length > 0) {
regionEls.forEach(el => el.setAttribute('data-view-cid', this.cid));
}
},

autoAddRegion(region, name, options) {
let regionEl = this.findRegionElement(name);

if (!regionEl || regionEl.length === 0) {
throw new MarionetteError({
name: 'NoMatchingRegion',
message: 'Cannot find a region named ${name} in the view ${this.cid}'
});
}

if (region) {
region._initEl = region.el = region.options.el = regionEl;
region.$el = region.getEl(regionEl);
} else {
region = this.addRegion(name, _.extend({}, this.getRegionDefaultOption(name, regionEl), options));
// temporary fixes missing aggressive reset
// https://github.com/marionettejs/backbone.marionette/issues/3540
const superReset = region.reset;
region.reset = function reset(opts) {
superReset.call(region, opts);
region.el = null;
return region;
};
return region;
}
},

};
1 change: 1 addition & 0 deletions src/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ const View = Backbone.View.extend({

this._renderTemplate(template);
this.bindUIElements();
this.idRegionElements();

this._isRendered = true;
this.triggerMethod('render', this);
Expand Down