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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:50:10+00:00 2026-06-07T20:50:10+00:00

Is there a good pattern for implementing a Backbone.js Router that responds differently for

  • 0

Is there a good pattern for implementing a Backbone.js Router that responds differently for these two events:

  • Direct navigation – A user navigates directly to page by putting the url directly into the browser or in the application via a call to Router.navigate()
  • Browser “back” button – The user presses the “back” button to go to a page that they were at previously.

The reason I ask is because if the router is called as a result of the user pressing the “back” button, I’d like to just put the old view back into its element via $('element_id').html(view.el) without the reloading data into the model and re-rendering the view.

If the router is called as a result of the user navigating directly to the page by entering the url into the browser, clicking on a link, or a call to Router.navigate() from within the application, I would like the router to instruct the model to re-request data and trigger a re-rendering of the view.

Any help and suggestions would be appreciated, thanks so much!

  • 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-07T20:50:12+00:00Added an answer on June 7, 2026 at 8:50 pm

    I faced a similar problem a while back. This may be more than what you want, but here’s what I found works.

    Direct navigation

    For direct navigation, the usual flow is something like this:

    • Initialize the app: Most backbone.js projects will have some code that initializes the app (App.init). Although a lot of examples insert the JSON for the collection (todos, etc.) in the actual HTML, I personally like to use this code as an opportunity to fetch the collection after the page has loaded, something like this (coffeescript):

      window.App =
        ...
        init: ->
          @todos = new App.Todos()
          @todos.deferred = @todos.fetch()
      
          @threads.deferred.done ->
            App.appRouter = new App.AppRouter(collection: self.todos)
            ...
      

      (The use of jQuery’s deferred is to make sure that the collection is fetched before actually rendering the page.)

    • Initialize the router: Here you get a chance to assign an element to the router and assign the collection to the router (I’m using the ‘SwappingRouter’ from thoughtbot’s backbone-support, which I highly recommend checking out):

      App.Router = Support.SwappingRouter.extend
      ...
        initialize: (options) ->
          @el = $('.content')
          @collection = options.collection
      
    • Execute the route handler: This is the last step, at which point the collection is already initialized and the router has a pointer to it, so we just have to create the view and render it:

        show: (id) ->
          view = new App.TodosView(model: @collection.get(id))
          @swap(view)
      

      (Swap renders the view and does a few other things to clean up.)

    Browser back button

    In this case, neither App.init nor AppRouter.initialize is called, so the collection by default won’t be reloaded. Backbone will automatically call navigate on the previous route, so depending on what’s in your route handler, the view may be re-rendered. In the example above, it would be (swap calls render), but you could work around this.

    The key problem here and always with backbone is that you’re working with a state-less (HTTP) protocol and a state-ful (rich client-side app) environment at the same time. Lining the two up so they work well together can be pretty tricky.

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

Sidebar

Related Questions

There's lots of good material on Design Patterns out there that covers how you
Is there a good gem to track/log user activities on the site? Like when
Is there a good C library that I can use in my client application
Is there a good pattern in Python to use for parsing multiple lines of
When working with databound controls in asp.net, is there a good pattern for making
Is there any good practice (pattern) in turning asynchronous calls into synchronous? I have
Are there some good resources tutorials or anyone has tried to implement a Capcha
Is there any good library which supports WebSockets and is compatible with GWT? Ideally,
Are there any good technical comparisons available between the various game development options available.
Is there a good C library for graph theoretic manipulations? I particularly need to

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.