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 7782977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:42:34+00:00 2026-06-01T19:42:34+00:00

I’m brand new to Backbone.js, and I’m trying to integrate it into my current

  • 0

I’m brand new to Backbone.js, and I’m trying to integrate it into my current rails app. I’m following the tutorials from railscasts, but I’m having trouble with integrating them. Basically, I’m at the point where I’m trying to call my resource (projects), and just display them on the page. My router looks as follows:

class Placeholder.Routers.Projects extends Backbone.Router
  routes:
    '': 'index'
    'projects/:id':'show'

  initialize: ->
    @collection = new CIS196Project.Collections.Projects()
    @collection.fetch()

  index: ->
    view = new Placeholder.Views.ProjectsIndex()
    $('#container').html(view.render().el)

  show: (id) ->
    alert "Project #{id}"

and my view looks like:

class Placeholder.Views.ProjectsIndex extends Backbone.View

  template: JST['projects/index']

  initialize: ->
    document.write(@collection)
    @collection.on('reset', @render, this)

  render: ->
    $(@el).html(@template(projects: @collection))
    this

all of which I mostly took straight from the tutorial. I added the document.write(@collection) to test.

Upon loading the page, I get the error:

projects_index.js:17 Uncaught TypeError: Cannot call method ‘on’ of undefined

I’m successfully making the fetch() in the router, because in my logs, the GET request successfully passes through, and on trying it in chrome’s console, the fetch should return an array of size 1, but instead, it gives that error, and then loads the default rails view from its controller (I’m also not sure why this occurs. I have a template that is defined, and yet right when it throws this error, it just reverts back to the previous, unless I call document.write). Upon calling document.write, @collection comes out as ‘undefined’.

Thanks for any help any of you could give!

  • 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-01T19:42:36+00:00Added an answer on June 1, 2026 at 7:42 pm

    You are creating your collection as an instance variable in your router:

    class Placeholder.Routers.Projects extends Backbone.Router
      #...
      initialize: ->
        @collection = new CIS196Project.Collections.Projects()
        #...
    

    but you never give that collection to your view:

    index: ->
      view = new Placeholder.Views.ProjectsIndex()
      #... 
    

    If you supply the collection to your view:

    index: ->
      view = new Placeholder.Views.ProjectsIndex(collection: @collection)
      #...
    

    then you’ll have a collection instance variable in your view.

    From the fine manual:

    constructor / initialize new View([options])

    […] There are several special options that, if passed, will be attached directly to the view: model, collection, el, id, className, tagName and attributes.

    So just specifying the collection option when you instantiate the view is sufficient to get a @collection in the view.

    As an aside, console.log(@collection) is a better idea than document.write(@collection), document.write has unfortunate side effects (like sometimes erasing the whole page) and console.log will produce more readable output.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is

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.