backbone.js中的下划线.js模板在tr周围添加div
我使用的是backbone.js中的下划线.js模板功能,我在页面中定义了以下模板,如下所示:backbone.js中的下划线.js模板在tr周围添加div,backbone.js,underscore.js,Backbone.js,Underscore.js,我使用的是backbone.js中的下划线.js模板功能,我在页面中定义了以下模板,如下所示: <script type="text/template" id="businessunit_template"> <tr data-uid="{{Uid}}"> <td class="first"><span>{{Name}}</span></td> <td class="{{StatusClass}}
<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>
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>
中央网络
4%
我只是看不出我做错了什么。有人知道这可能是从哪里来的吗?这看起来很像在
视图中没有正确声明.el
属性时得到的那种错误DOM片段。我将在ReportBusinessUnitView.render()
中放置一个断点/debugger
语句,并从那里检查this.el
属性的值。(文件)
此外,请检查您的代码:
- 您是否声明了
.el
属性?(例如在MIBaseView
中)
- 它是否命中了正确的DOM节点
如果没有,主干会自动为您创建DIV
节点,这可能会让您感到困惑。我认为,在模板周围包含默认DIV标记是一种安全措施。这将提供一个父标记,视图的事件将附加到该父标记。这样,事件传播将按预期工作。同样,如果您放弃一个视图并删除插入的HTML,所有事件都将随之发生,从而允许垃圾收集器完成其工作
我最近非常伤心,因为我将.el设置为静态HTML父节点(以防止添加默认DIV)。因为即使在我的动态HTML被替换之后,它仍然存在,所以事件仍然围绕着对动作的响应和制造普遍的混乱 万分感谢,我以为我疯了。如果我将标记名“tr”添加到视图中,并从模板中删除封闭标记,那么一切都很好。您可能想看看哪个标记名允许您将
保留在模板中;这使得在其上设置数据uid
属性更容易。
<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>