Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8038745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:20:17+00:00 2026-06-05T03:20:17+00:00

I have a Rails project with Backbone.js and HAML as client side templating language.

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T03:20:19+00:00Added an answer on June 5, 2026 at 3:20 am

    The Collection#fetch method 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:

    The options hash takes success and error callbacks which will be passed (collection, response) as arguments. When the model data returns from the server, the collection will reset.

    So you can use the callbacks:

    render: ->
      @collection.fetch(
        success: (collection, response) =>
          @$el.html(@template(collection: @collection))
      @
    

    Or you can bind the view’s render to the collection’s reset event:

    class window.MeetingIndex extends Backbone.View
      template: JST['meeting/index']
      initialize: ->
        @collection.on('reset', @render)
        @collection.fetch()
      render: =>
        @$el.html(@template(collection: @collection))
        @
    

    Then the fetch call in the view’s initialize would indirectly trigger the proper render call 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”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a rails project, the views only consist with HTML.ERB files, my client
I have a rails-backbone project that generates jst.ejs templates. I'd like to include some
I'm using backbone with the backbone-rails gem which does its own templating and project
I have a Rails project that is basically a simple web app for a
I have a Rails project and use RSpec as a testing framework. What I
I have a rails project that I would like to set up email notifications
I currently have a rails project that looks to let customers authenticate their account
I have a Rails 3 project. With Rails 3 came Arel and the ability
In my Rails project I have the following code: auto_link( h( wrap_long_string(post.text,50) )).gsub(/\n/,<br />)
In my rails project I have a presenter method that has a helper view

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.