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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:44:04+00:00 2026-06-03T04:44:04+00:00

So basically I’ve been toying with this pattern of pubsub with subviews re-rendering when

  • 0

So basically I’ve been toying with this pattern of pubsub with subviews re-rendering when called by parent ‘controller’ view (sorry if that’s confusing). If the subviews are based on a fetch of a collection or model (not shown below), sometimes they aren’t rendered in the correct order I need them too, ie if SubView1 is a small nav for Subview2, I don’t want it below SubView2.

I figure there has to be a pattern for such a common problem. Lemme know if this doesn’t make sense and I will clarify. But the basic situation I’m dealing with is below.

markup:

 <div id="main-container">
   <div class="inner-container"></div>
 </div>

js:

 var ToggleNavView = Backbone.View.extend({
    //let's say this template has two links
    template: Handbars.compile(linkstmpl),

    el: $('#main-container'),
    events: {
      "click a": "toggleViews"
    }
    initialize: function(){
      _.bindAll(this, 'render', 'toggleViews');

      // whoa, nice looking event aggregator http://bit.ly/p3nTe6
      this.vent = _.extend({}, Backbone.Events);

      this.render();
    },
    render: function(){
       $(this.el).append(this.tmpl);

       // suppose subviews below are declared as modules above with, say, requirejs
       var sub1 = new SubView1({ vent: this.vent }),
           sub2 = new SubView2({ vent: this.vent });


    },

    toggleViews: function(e){
      e.preventDefault();

      // get name of section or view you're toggling to
      var section = $(e.currentTarget).data('section');

      // publish events to subscribers
      this.vent.trigger('toggleInboxViews', this, section);


    },

   });

   var SubView1 = Backbone.View.Extend({
      template: Handlebars.compile(tmpl1),
      initialize: function(ops){
        _.bindAll(this, 'render', 'removeOrRerender');

        this.vent = ops.vent || null;
        this.render()
      },
      render: function(){
        $(this.el).append(this.template()).appendTo($(".inner-container"))
      },

      removeOrRerender: function(obj, section){
        if( section == 'my-section'){
          this.render();
        } else if( section == 'other-section' ) {
          $(this.el).fadeOut();
        }
      },

    })

    // another subview with same functionality etc... 

    // init view
    new ToggleNavView();
  • 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-03T04:44:05+00:00Added an answer on June 3, 2026 at 4:44 am

    If you need your sub-views to show up in specific places then the parent view should define that structure and the sub-views should be told where to render themselves. Then it won’t matter what order things are draw in as the overall structure doesn’t change.

    For example, if you want one sub-view to appear at the top and the other below it, the main view should look like this:

    <div id="main-view">
        <div id="sub1"></div>
        <div id="sub2"></div>
    </div>
    

    then the main view would render the sub-views with something like this:

     var sub1 = new SubView1({ el: this.$el.find('#sub1'), vent: this.vent }),
     var sub2 = new SubView2({ el: this.$el.find('#sub2'), vent: this.vent });
    

    By specifying the el for the sub-views, their location on the page is no longer their problem and they won’t shift positions if they’re rendered in a different order. A happy side effect of this structure is that the sub-views are only concerned with themselves and are nicely self-contained; the parent view just needs to put the pieces in the right place by structuring its template properly and everything just works.

    Here’s a simple demo that might clarify the structure: http://jsfiddle.net/ambiguous/9u3S5/

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

Sidebar

Related Questions

Basically from a database I am getting data that is formatted like this nameofproject101
Basically I have an iframe loaded that is accessed from the parent whenever it
Basically, I have a main page (parent page) with a link that opens up
Basically this function is meant to store the height value of the element that
Basically I have a few functions that look like this: class MyClass { void
Basically I have some variables that I don't want to preinitialize: originalTime = None
basically i have (state, state code) pairs, that are subsets of country [USA] ->
Basically what I need to do is write a function that takes in a
Basically I'd be looking to implement a method like this. IQueryAble GetQuery<T>(Entities db) or
Basically there is a file called 8puzzle.py and I want to import the file

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.