Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Underscore.js templates in backbone.js adding a div around a tr

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?

like image 375
dagda1 Avatar asked Nov 14 '11 10:11

dagda1


1 Answers

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.

like image 148
Jacob Oscarson Avatar answered Nov 15 '22 09:11

Jacob Oscarson