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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:26:24+00:00 2026-06-01T12:26:24+00:00

Previously, my Backbone router looked like this: class App.Routers.ThingsRouter extends Backbone.Router routes: ”: ‘index’

  • 0

Previously, my Backbone router looked like this:

class App.Routers.ThingsRouter extends Backbone.Router
  routes: '': 'index'
  routes: 'previews/:id': 'show'

  initialize: ->
    @collection = new App.Collections.ThingsCollection
    @collection.fetch

  index: ->
    view = new App.Views.ThingsIndex(collection: @collection)
    $('#app-container').html(view.render().el)

  show: (id) ->
    @model = @collection.get(id)
    view = new App.Views.ThingsShow(model: @model)
    $('#app-container').html(view.render().el)

When navigating to http://localhost, I would get the index view rendered, and when clicking on individual elements, I would get the show view rendered. However, if I went to http://localhost/things/1 directly (i.e. by typing in the URL), the show view wouldn’t be rendered. I realized that this was because the view was being rendered before @collection.fetch was completing. I changed my router to the following:

class App.Routers.ThingsRouter extends Backbone.Router
  routes: '': 'index'
  routes: 'previews/:id': 'show'

  initialize: ->
    @collection = new App.Collections.ThingsCollection

  index: ->
    @collection.fetch success: =>
      view = new App.Views.ThingsIndex(collection: @collection)
      $('#app-container').html(view.render().el)

  show: (id) ->
    @collection.fetch success: =>
      that.model = that.collection.get(id)
      view = new App.Views.ThingsShow(model: @model)
      $('#app-container').html(view.render().el)

Which works fine. However, there’s obviously a little latency as the collection is re-fetched every time I switch routes. Is this good Backbone practice? Not sure if there’s a better way of doing this.

  • 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-01T12:26:26+00:00Added an answer on June 1, 2026 at 12:26 pm

    This is a great use case for jQuery’s Deferred() method.

    Just create a Deferred object and attach it to the router. Then fetch the collection in the initialize method and call resolve() on the Deferred object. Your index and show methods can subscribe to the done callback and instantiate the view. This done callback won’t be ran until the collection has been fetched. And if it has already been fetched, then it runs immediately.

    class App.Routers.ThingsRouter extends Backbone.Router
      routes: '': 'index'
      routes: 'previews/:id': 'show'
    
      initialize: ->
        @collectionFetched = new $.Deferred
        @collection = new App.Collections.ThingsCollection
        @collection.fetch success: ->
          @collectionFetched.resolve()
    
      index: ->
        that = this
        @collectionFetched.done ->
          view = new App.Views.ThingsIndex(collection: that.collection)
          $('#app-container').html(view.render().el)
    
      show: (id) ->
        that = this
        @collectionFetched.done ->
          that.model = that.collection.get(id)
          view = new App.Views.ThingsShow(model: that.model)
          $('#app-container').html(view.render().el)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This example is borrowed from Backbone directory demo app https://github.com/ccoenraets/backbone-directory/blob/master/web/js/utils.js#L11 // The Template Loader.
Previously I did a Tutorial on OpenGL-ES 1.0. For reference, this can be found
Previously, I have an app that uses core data. I use same store url
I have simple user model: var user = Backbone.Model.extend({ initialize: function(){ this.bind(change:auth, function (){
Previously a class which I'm now rebuilding had a member variable $settings which was
Previously i was able to download YouTube videos as mp3 via youtube-mp3.org Using this
Previously I had: MyClass::MyClass() : myMember(VALUE,this,5,etc.) { ... } but I need to do
Previously i wrote an app that used reflection to serialize data from json to
Previously I read posts with same problem but my question persists. My app needs
Im using backbone, require.js and coffeescript in a application. Programming my app, discovered i

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.