I went through the Railscast tutorial and got it all working. Working on a quick prototype to see if Backbone is viable but I've messed something up and I'm not sure what I've done wrong. I'm on Backbone 1.
View
class Shsh.Views.AssetsIndex extends Backbone.View
template: JST['assets/index']
initalize: ->
@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)
The view gets rendered fine, but the length of @assets comes back as 0. I can go through the steps in the console and when I render the view again it comes back as being the correct length. What am I doing wrong?
EDIT:
I also do actually have a collection and model. The code there is all boilerplate generated by Backbone On Rails.
You are calling fetch()
too early -- in router creation. Should be called in specific route code instead. The way you implemented it, fetch and reset may complete before route is triggered and therefore you'll start listening to reset
after it has been fired
I'm an idiot. Initialize is spelt wrong in Shsh.Views.AssetsIndex.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.