javascript 阻止主干将周围的 div 添加到视图中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7096670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Stop backbone from adding surrounding divs to a view?
提问by egidra
I am using Handlebars.js as a templating tool for my Backbone.js app. My render functions in my views usually look like the following:
我使用 Handlebars.js 作为我的 Backbone.js 应用程序的模板工具。我的视图中的渲染函数通常如下所示:
var source = $("#round").html();
var template = Handlebars.compile(source);
var context = JSON.parse(JSON.stringify(this.model));
var html = template(context);
$(this.el).html(html);
return this;
The above is appended to the main app view through the following code(this is the code that calls the above code):
以上通过以下代码附加到主应用视图中(这是调用上述代码的代码):
$('div#round-container', this.el).append(roundView.render().el);
My Handlebars template handles all of the styling and layout, so I leave the "el" element of a view blank. Backbone.js automatically adds surrounding div tags around the Handlebars template. I assume this is because the "el" element is blank. Is there a way to prevent the addition of the surrounding div tags? Thanks!
我的 Handlebars 模板处理所有样式和布局,因此我将视图的“el”元素留空。Backbone.js 会自动在 Handlebars 模板周围添加周围的 div 标签。我认为这是因为“el”元素是空白的。有没有办法防止添加周围的 div 标签?谢谢!
回答by Caged
What's happening is this.el
is created on the fly because it was never explicitly set by you. You have two options:
正在发生的事情是动态this.el
创建的,因为它从未由您明确设置。您有两个选择:
You should specify the element you want to create with
tagName
,className
and/orid
and let backbone create that for you.In
render
you should setthis.el
to the contents of your handlebars template. So you would havethis.el = $(template(context))
.
您应该指定要创建的元素
tagName
,className
和/或id
让主干为您创建。在
render
您应该设置this.el
为您的把手模板的内容。所以你会有this.el = $(template(context))
.
The docs have an expanded explanation - http://documentcloud.github.com/backbone/#View-el
文档有一个扩展的解释 - http://documentcloud.github.com/backbone/#View-el
回答by Sandeep Kashyap
Another easy way is to append the childnodes of the rendered element instead
另一种简单的方法是附加渲染元素的子节点
$('div#round-container', this.el).append(roundView.render().el.childNodes);
回答by Pinelopi Kouleri
Or append the html of the rendered element, e.g.:
或者附加渲染元素的 html,例如:
$('div#round-container', this.el).append(roundView.render().el.html());