EDIT3: Warning! This answer may be out of date. I received a comment that this answer no longer works and have not had the time to investigate (I personally do not use this method).
I like to use Twitter/Bootstrap as my UI library and was having some issues with table styling because of the default tag wrapping (specifically a <div>
between my <tbody>
and my <tr>
s).
After some digging, I found something in the Marionette docs on the Region
about how the View
attaches the el
. Backbone builds the el
from various View
attributes and keeps the built element up to date so it can be rendered at any time. So I figured, if view.el
is the parent, why not just use the HTML contents? Marionette also provides a way to create a custom Region
I was able to get everything running by creating a custom Region
with an overridden open
function. That way I can specify which regions I want to wrap with a tag and those that I do not. In the following example snippet, I create my custom non-wrapping Region
(NowrapRegion
) and use it in my Layout
(MyLayout
) for my <tbody>
(the views I pass in my real program create <tr>
s).
var NowrapRegion = Marionette.Region.extend({
open: function(view){
this.$el.html(view.$el.html());
}
});
var MyLayout = Marionette.Layout.extend({
template: templates.mylayout,
regions: {
foo: '#foo',
columns: '#columns', //thead
rows: { selector: '#rows', regionType: NowrapRegion } //tbody
}
});
BOOM! Wrapping when I want it and none when I don't.
EDIT: This seems to affect events
applied at the *View
level. I haven't looked into it yet, but be warned that they don't seem to be getting triggered.
Whether this is the "right" way to do it or not, I am not sure. I would welcome any feedback from @derick-bailey should he see this.
EDIT2: @chris-neale suggested the following, I have not verified this yet but it seems sound. Thanks!
Rather than copying the html in the view, using a deep clone on the
child nodes will maintain events and data.
var NowrapRegion = Marionette.Region.extend({
open: function(view){
view.$el.children().clone(true).appendTo(this.$el);
}
});