在backbone.js中的Underscore.js模板在tr周围添加一个div
我正在使用 backscore.js 来自backbone.js的模板功能,我在页面中定义了以下模板,如下所示:
<script type="text/template" id="businessunit_template">
<tr data-uid="{{Uid}}">
<td class="first"><span>{{Name}}</span></td>
<td class="{{StatusClass}} tac">{{OverallScore}}%</td>
<td>
<a class="impactanalysis individualBu" href="#"> </a>
</td>
</tr>
</script>
我将 trs 附加到下表的 tbody 元素:
<table class="listing">
<thead>
<tr>
<th class="first">Business Units</th>
<th>BCMS<br />Status</th>
<th>View</th>
</tr>
</thead>
<tbody id="reportBusinessUnits"></tbody>
</table>
我呈现 tr 的个人主干视图看起来像这个:
class ReportBusinessUnitView extends MIBaseView
initialize: (options) ->
@vent = options.vent
@template = _.template($('#businessunit_template').html())
events:
"click .individualBu": "showBusinessUnitDetail"
showBusinessUnitDetail: (e) =>
e.preventDefault()
self = @
@vent.trigger('management:showbusinessunitdeail', @model)
render: =>
$(@el).html(@template(@model.toJSON()))
@
问题是,渲染的输出在 tr 周围有一个 div,我不知道它来自哪里:
<div>
<tr data-uid="a5e3c218-1ca4-4806-b27e-24a25ed83ab6">
<td class="first"><span>Central Networks</span></td>
<td class="red tac">4%</td>
<td>
<a class="impactanalysis individualBu" href="#"> </a>
</td>
</tr>
</div>
我只是看不到我做错了什么。有人知道这可能来自哪里吗?
I am using underscore.js's templating capabilities from backbone.js, I have the following template that I define in my page like this:
<script type="text/template" id="businessunit_template">
<tr data-uid="{{Uid}}">
<td class="first"><span>{{Name}}</span></td>
<td class="{{StatusClass}} tac">{{OverallScore}}%</td>
<td>
<a class="impactanalysis individualBu" href="#"> </a>
</td>
</tr>
</script>
I am attaching the trs to the tbody element of following table:
<table class="listing">
<thead>
<tr>
<th class="first">Business Units</th>
<th>BCMS<br />Status</th>
<th>View</th>
</tr>
</thead>
<tbody id="reportBusinessUnits"></tbody>
</table>
My individual backbone view that renders the tr looks like this:
class ReportBusinessUnitView extends MIBaseView
initialize: (options) ->
@vent = options.vent
@template = _.template($('#businessunit_template').html())
events:
"click .individualBu": "showBusinessUnitDetail"
showBusinessUnitDetail: (e) =>
e.preventDefault()
self = @
@vent.trigger('management:showbusinessunitdeail', @model)
render: =>
$(@el).html(@template(@model.toJSON()))
@
The problem is, the rendered output has a div around the tr and I have no idea where it is coming from:
<div>
<tr data-uid="a5e3c218-1ca4-4806-b27e-24a25ed83ab6">
<td class="first"><span>Central Networks</span></td>
<td class="red tac">4%</td>
<td>
<a class="impactanalysis individualBu" href="#"> </a>
</td>
</tr>
</div>
I just cannot see what I am doing wrong. Has anybody any idea where this could be coming from?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这看起来非常像当您没有在
View
中正确声明.el
属性时得到的错误 DOM 片段。我会在ReportBusinessUnitView.render()
中放置一个断点/debugger
语句,并从那里检查this.el
属性的值。 (View.el 文档)。另外,检查您的代码:
.el
属性? (例如在MIBaseView
中)如果没有,Backbone 会自动为您创建
DIV
节点,这可能会令人困惑。That looks very much like the kind of faulty DOM fragment you get when you haven't declared the
.el
attribute in aView
correctly. I'd put a breakpoint/debugger
statement inReportBusinessUnitView.render()
and inspect the value of thethis.el
attribute from there. (View.el docs).Also, check your code:
.el
property? (inMIBaseView
for example)If not, Backbone auto creates the
DIV
node for you, which can be confusing.我相信,在模板周围包含默认的 DIV 标签是一种安全措施。这给出了视图事件所附加的父标签。这样事件传播就会按预期进行。同样,如果您丢弃视图并删除插入的 HTML,所有事件都会随之发生,从而允许垃圾收集器完成其工作。
我最近有相当大的悲伤,因为我将 .el 设置为静态 HTML 父节点(为了防止添加默认 DIV)。因为即使在我的动态 HTML 被替换之后它仍然存在,事件仍然围绕着响应操作并造成普遍的混乱!
The inclusion of a default DIV tag to surround the template is, I believe, a safety measure. This gives a parent tag to which the view's events are attached. That way event propagation will work as expected. As well, if you discard a view and remove the inserted HTML all events will go with it allowing the garbage collector to do its job.
I recently had considerable grief because I set the .el to the static HTML parent node (in order to prevent the default DIV from being added). Since it remained even after my dynamic HTML was replaced the events were still around responding to actions and creating general chaos!