I have a Backbone.js app and am trying to integrate with Backgrid, but I am having trouble understanding where I should be calling new Backgrid
. I tried calling it in my view after things get rendered but appending the grid doesn't work because things aren't actually rendered yet. Here is some code:
SpreadsheetIndex.js.coffee
D3.Views.SpreadsheetsIndex = Support.CompositeView.extend
initialize: (options) ->
this.tables = options.tables
this.resources = options.resources
_.bindAll(this, 'render')
render: ->
this.renderTemplate()
this.renderSpreadsheets()
resources = this.resources
this.tables.each (table) ->
subset = resources.subcollection
filter: (resource) ->
resource.escape('table_id') == table.escape('id')
grid = new Backgrid.Grid
columns: table.attributes.columns
collection: subset
$("#"+table.escape('id')).append(grid.render().$el);
return this
renderTemplate: ->
this.$el.html(JST['spreadsheets/index']({ spreadsheets: this.tables }))
renderSpreadsheets: ->
self = this
self.$('tbody').empty();
spreadsheets/index.jst.ejs
<% spreadsheets.each(function(spreadsheet) { %>
<h4><%= spreadsheet.escape('name')%></h4>
<div id='<%= spreadsheet.escape('id') %>'></div>
<% }) %>
The issue is that the $("#"+table.escape('id'))
selector does not select anything because the template hasn't rendered yet. It feels like I'm putting this in the wrong place. What am I doing wrong?