After completing the Railscast tutorial successfully, I decided to work on a quick prototype to test the viability of Backbone. Unfortunately, I seem to have made a mistake somewhere as things are not working as expected. I am using Backbone 1.
View
class Shsh.Views.AssetsIndex extends Backbone.View
template: JST['assets/index']
initialize: ->
@collection.on('reset', @render, this)
render: ->
$(@el).html(@template(assets: @collection))
console.log('rendered')
this
Router
class Shsh.Routers.Assets extends Backbone.Router
routes:
'': 'index'
initialize: ->
@collection = new Shsh.Collections.Assets()
@collection.fetch({reset: true})
index: ->
view = new Shsh.Views.AssetsIndex(collection: @collection)
$('#container').html(view.render().el)
Although the view is rendered correctly, the length of @assets is returning as 0. Strangely, when I repeat the steps in the console and re-render the view, it shows the correct length. Any ideas on what could be causing this issue?
EDIT:
I do have a collection and model in place. The code included here is all boilerplate generated by Backbone On Rails.