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

  • Home
  • SEARCH
  • 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 7843905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:42:30+00:00 2026-06-02T16:42:30+00:00

I’m having some difficulty with backbone js. Currently, I have a rails app that

  • 0

I’m having some difficulty with backbone js. Currently, I have a rails app that is giving information to backbone via json. It appears that this is all function correctly because when I navigate to the URL, the json data is displayed. I’m having difficulty when I am using the fetch method. Below is my code:

jQuery ->
  class Event extends Backbone.Model
    url: '/events/get_last'
    defaults:
      name: 'This isnt from the server'
      date_of: '2012-03-01'
      max_attendees: '300'

  class EventList extends Backbone.Collection
    url: '/events/get_events'
    model: Event

  class EventView extends Backbone.View
    el: $ 'body'
    model: Event
    initialize: (options) -> 
      _.bindAll @
      @event = new Event
      @event.fetch()
      @render()
    render: ->
      $(@el).append "<span>#{@event.get 'name'}"


  class EventRouter extends Backbone.Router
    routes : 
        '' : 'home'
        'dashboard' : 'render_data_view'
        'default' : 'default_view'
    home: -> 
      console.log "home"

    render_data_view: ->
      event_view = new EventView

    default_view: ->
      console.log 'the default view was hit'

    initialize: ->
      Backbone.history.start(pushState: true)

  event_router = new EventRouter
  • 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-02T16:42:34+00:00Added an answer on June 2, 2026 at 4:42 pm

    Your EventView should bind to its model’s "change" event so that fetch will trigger things automatically:

    fetch model.fetch([options])

    […] A "change" event will be triggered if the server’s state differs from the current attributes.

    So your view should look more like this:

    class EventView extends Backbone.View
      el: 'body'
      initialize: (options) ->
        @model = new Event
        @model.on 'change', @render
        @model.fetch() # This will call `render` automatically.
      render: =>
        @$el.append "<span>#{@model.get 'name'}</span>"
    

    A few notes:

    1. You rarely need to use bindAll with CoffeeScript, use the fat arrow (=>) instead.
    2. The model property on views should be set per-instance to a single model (hence the @event -> @model name change). Collections get their model property set in the class definition and the @model for a collection should be a model class.
    3. Binding to the "change" event on the model means that your view will be notified (i.e. @render will be called) whenever anyone changes the model, this event behavior is sort of the whole point of Backbone so you should use it, the per-fetch success and error handlers are more for error handling and reporting success to the user.
    4. You really should close your <span> elements.
    5. The view already has @$el so you might as well use that instead of $(@el).
    6. You can use el: 'body' instead of el: $ 'body' and leave the jQueryification to Backbone.
    7. If you’re using an older Backbone then:
      1. You might have to use bind instead of on.
      2. You might not have @$el so $(@el) might be needed instead.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't

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.