在backbone.js中的Underscore.js模板在tr周围添加一个div

发布于 2024-12-15 12:30:39 字数 1762 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

虚拟世界 2024-12-22 12:30:39

这看起来非常像当您没有在 View 中正确声明 .el 属性时得到的错误 DOM 片段。我会在 ReportBusinessUnitView.render() 中放置一个断点/debugger 语句,并从那里检查 this.el 属性的值。 (View.el 文档)。

另外,检查您的代码:

  • 您是否声明了 .el 属性? (例如在 MIBaseView 中)
  • 它是否命中了正确的 DOM 节点?

如果没有,Backbone 会自动为您创建 DIV 节点,这可能会令人困惑。

That looks very much like the kind of faulty DOM fragment you get when you haven't declared the .el attribute in a View correctly. I'd put a breakpoint/debugger statement in ReportBusinessUnitView.render() and inspect the value of the this.el attribute from there. (View.el docs).

Also, check your code:

  • Have you declared an .el property? (in MIBaseView for example)
  • Does it hit the right DOM node?

If not, Backbone auto creates the DIV node for you, which can be confusing.

成熟稳重的好男人 2024-12-22 12:30:39

我相信,在模板周围包含默认的 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文