we are using Backbone,Marionette and handlebars
for my application. When I try to render my view inside Marionette.Region
, one extra div
wrapping around the template. How can I avoid this.
html code :
<div id="mainDiv"></div>
<script type="text/x-handlebars-template" id="basic">
<div id="first"></div>
<div id="second"></div>
</script>
js code :
//function
var templateCompilation=function(templateId,data){
var alertCompilation=Handlebars.compile(document.getElementById(templateId).innerHTML);
return alertCompilation(data);
};
//Application
myApp = new Backbone.Marionette.Application();
myApp.addRegions({
mainContainer:"#mainDiv"
});
myApp.start();
//first view
var basicView=Marionette.ItemView.extend({
template:function(){
return templateCompilation("basic",{});
}
});
//reding view
var basicViewObj=new basicView();
myApp.mainContainer.show(basicViewObj);
To avoid extra div, I try with following statements my bad luck nothing working.
var basicViewObj=new basicView({el:$("#mainDiv")});
var basicViewObj=new basicView({tagName:""});
can anyone help me.
Important Update:
The code below the line will not work. I wrote it without testing it (was on my mobile). The following update has been tested and incorporated the important comment suggested by
@vzwick
.As explained below, override
attachHtml
in the Region. We are going to three important changes toattachHtml
as annotated in the comments belowThe important thing to note is that the OP's
basicView
now shares itsel
withmyApp.mainContainer
. SincemyApp.mainContainer
is a Region and it does not take anevent
parameter there isn't a chance of there being conflicts if events are re-delegated. The same is true if this is used with aLayoutView
since the re-delegation of events would happen to theLayoutView
Region
el
, and not theLayoutView
el
, then there should be no conflicts.Beginning of old post
I haven't tried this before, but I'm sensitive to your problem from a functional and structural level. What I suggest you do is override the
Region
'sattachHtml
function.By default
Backbone.Marionette
'sattachHtml
is doing thisTo change this functionality you could define a new
attachHtml
in yourRegion
like the following:Now the
Region
el
will directly append the inner nodes of the view that you're showing in that region.To override the original
attachHtml
of themainContainer
region you would use yourApplication
variable, i.e.with the code example written above.