I have a Rails project with Backbone.js and HAML as client side templating language.
in file app/assets/views/meeting.coffee:
class window.MeetingIndex extends Backbone.View
template: JST['meeting/index']
render: ->
@collection.fetch()
@$el.html(@template(collection: @collection))
this
in file app/assets/javascripts/templates/meeting/index.hamlc
- console.log(@collection.length) # prints 0 in console
- console.log(@collection.models) # prints [] in console
- console.log(@collection.at(0)) # prints undefined in console
- window.x = @collection
if I go to the browser console I get:
x.length # returns 2
x.models # returns [Meeting, Meeting]
x.at(0) # returns Meeting object
If I can access @collection variable in .hamlc file, because I am assigning it to window.x. Why can’t I access the @collection items from the .hamlc file?
I need something like
- for model in @collection.models
%p= model.get('landlord_id')
%p= model.get('tenant_id')
%p= model.get('at')
to work
The
Collection#fetchmethod is asynchronous (i.e. it is an AJAX call behind the curtains) so@collection.fetch()hasn’t gotten anything back from the server when you try to use it in your view. However:So you can use the callbacks:
Or you can bind the view’s
renderto the collection’sresetevent:Then the
fetchcall in the view’sinitializewould indirectly trigger the properrendercall when it gets something back from the server. This approach works best if your template knows what to do with an empty collection, perhaps it could detect an empty collection and display a “loading” message or just say “nothing’s here yet”.