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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T18:20:36+00:00 2026-05-29T18:20:36+00:00

The Application Layout I have an application, with a sidebar that holds many items

  • 0

The Application Layout

I have an application, with a sidebar that holds many items and a main div which displays these items. There is also a simple Backbone.Router, a ItemsCollection and a Item model. I’ve got a SidebarView for the sidebar and a ShowView to show the selected item.

                  +-------------------------+
                  | http://app.de/#/show/3  |   <-- Current URL
                  +-------------------------+
                  | Sidebar | Main          |
                  |---------+---------------|
                  | Item 1  |               |
 SidebarView -->  |---------|    Display    |
                  | Item 2  |    Item  3    | <-- MainView handled by
                  |---------|    here       |          MainRouter
Selected Item --> | Item 3 *|               |
                  +---------+---------------+

On startup, I initialize the SidebarView and the MainRouter. The SidebarView attaches its render method to the ItemCollection#all event. I also attach the ItemCollection#refresh event to Backbone.history.start(), then I fetch the ItemCollection.

$(function() {
  window.router = new App.MainRouter;
  window.sidebar = new App.SidebarView;
  ItemCollection.bind("reset", _.once(function(){Backbone.history.start()}));
  ItemCollection.fetch();
});

The problem

I want to highlight the currently selected item. This works by binding the route.show event from the router:

# I removed all code which is not necessary to understand the binding
class SidebarView extends Backbone.View
  el: ".sidebar"

  initialize: () ->
    window.router.bind 'route:show', @highlight_item

  # The route is routed to #/show/:id, so I get the id here
  highlight_item: (id) ->
    $(".sidebar .collection .item").removeClass("selected")
    $("#item-" + id).addClass("selected")

It works perfectly when I select an Item when the app is loaded. But when the page is loaded with #/show/123 as the URL, the item is not highlighted. I run the debugger and found out, that the sidebar is not rendered yet, when the highlight_item callback is invoked.

Possible solutions

Is there any way to reorder the bindings, so that the Item#refresh event invokes SidebarView#render first and then start the routing?

Maybe a workaround that just takes the current route from the window.router (I did not find any method in the Backbone Docs) and highlights the Item when its rendered?

Or is my initialization just stupid and should I handle things differently?

  • 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-05-29T18:20:38+00:00Added an answer on May 29, 2026 at 6:20 pm

    You could do two things:

    1. highlight_item could keep track of which item is supposed to be highlighted.
    2. Update your render to initialize the highlighted item.

    Something like this:

    initialize: () ->
      @highlighted_id = null
      window.router.bind 'route:show', @highlight_item
    
    render: () ->
      # Set everything up inside @el as usual
      @highlight_current()
      @
    
    highlight_item: (id) =>
      @highlighted_id = id
      @highlight_current()
    
    highlight_current: () ->
      return unless(@highlighted_id)
      $(@el)
        .find('.selected').removeClass('selected')
        .end()
        .find("#item-#{@highlighted_id}").addClass('selected')
    

    So, as long as highlight_item gets called, highlight_current will also get called with the appropriate @highlighted_id set and everything should work out.

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

Sidebar

Related Questions

I have an application that has a primary layout of portrait (it is fixed
I have the following layout with a sidebar that is only present on some
I have a MVC3 application, based on default layout from VS 2010, which I
I have an Application layout that is based on a treeView at the left,
In my application.html.erb layout for my app, I want to have a partial that
In my CakePHP application layout I have a section for latest posts that needs
I have a simple shopping cart application that displays the session's shopping cart with
I have the following in my application layout page <body> <div class=container showgrid> <%=
I have an application layout in my rails app to give a default header/footer
I have two libraries and a single application assembly project layout and I'm wondering

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.