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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:27:50+00:00 2026-05-26T03:27:50+00:00

I’m running around in circles seemingly missing something in my current app implementing backbone.js.

  • 0

I’m running around in circles seemingly missing something in my current app implementing backbone.js. The problem is I have a master AppView, which initializes various subviews (a graph, a table of information, etc) for the page. My desire is to be able to change the layout of the page depending on a parameter flag passed along while navigating.

What I run into is a case where the subviews, which have reference to dom elements that would be in place after the template is rendered, do not have access to those elements during the main AppView initialization process. So the main question is how do I ensure that the proper dom elements are in place for each event bind process to get setup properly?

With the following code if I have an event bound to a model change within my LayoutView, the layout gets rendered but the subsequent views do not properly render. Some thing I have fiddled with has been to set all the views ‘.el’ values to a static element within the html structure. This gets rendering to occur, though that seems to break away from the nice scoping ability provided by utilizing ‘.el’.

Sample Code:

//Wrapped in jquery.ready() function...
//View Code
GraphView = Backbone.View.extend({ // init, model bind, template, supporting functions});

TableView = Backbone.View.extend({ // init, model bind, template, supporting functions});

LayoutView = Backbone.view.extend({
  initialize: function() {
    this.model.bind('change:version', this.render, this);
  },
  templateA = _.template($('#main-template').html()),
  templateB = _.template($('#secondary-template').html()),
  render: function() {
    if ('main' == this.model.get('version')) {
      this.el.html(this.templateA());
    } else {
      this.el.html(this.templateB());
    }
  },
});

MainView = Backbone.View.extend({
  initialize: function() {
    this.layoutView = new LayoutView({
      el: $('#layoutContainer'),
      model: layout,
    });
    this.graphView = new GraphView({
      el: $('.graphContainer'), 
      model: graph
    });
    this.tableView = new TableView({
      el: $('#dataTableContainer', 
      model: dataSet
    });
  },
});

// Model Code
Layout = Backbone.Model.extend({
  defaults: {
    version: 'main',
  },
});

Graph = Backbone.Model.extend({});
dataSet = Backbone.Model.extend({});

// Router code...

// Fire off the app
AppView = new MainView($('#appContainer'));
// Create route, start up backbone history

HTML:
For simplicity I just rearranged the divs between the two layouts.

<html>
  <head>
    <!-- include above js and backbone dependancies -->
  </head>
  <body>
    <script type="text/template" id="main-template">
      <div class="graphContainer"></div>
      <div class="dataTableContainer"></div>
      <div>Some other stuff to identify that it's the main template</div>
    </script>
    <script type="text/template" id="secondary-template">
      <div class="dataTableContainer"></div>
      <div>Rock on layout version two!</div>
      <div class="graphContainer"></div>
    </script>
    <div id="appContainer">
      <div id="nav">
        <a href="#layout/main">See Layout One</a>
        <a href="#layout/secondary">See Layout Two</a>
      </div>
      <div id="layoutContainer"></div>
    </div>
  </body>
</html>

Appreciate insight/input that any of you guys may have. Thanks!

  • 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-26T03:27:51+00:00Added an answer on May 26, 2026 at 3:27 am

    You’ve got a lot of missing code in your sample, but if I understand correctly, the issue is that you are trying to render views that depend on HTML generated by LayoutView, but the layout is being chosen asynchronously when the model is updated, so LayoutView hasn’t been rendered yet.

    There are (as with pretty much everything Backbone-related) multiple approaches to this, but in general:

    • If I have views who depend on a “parent” view being rendered, I’ll put the responsibility for instantiating and rendering those views in the parent view – in this case, LayoutView, not MainView.

    • If the parent is being rendered asynchronously, it needs to perform that instantiation in the callback – here, the LayoutView.render() method.

    So I might structure the code like this:

    LayoutView = Backbone.view.extend({
      initialize: function() {
        this.model.bind('change:version', this.render, this);
        // you probably want to call this.model.fetch() here, right?
      },
      templateA: _.template($('#main-template').html()),
      templateB: _.template($('#secondary-template').html()),
      render: function() {
        if ('main' == this.model.get('version')) {
          this.el.html(this.templateA());
        } else {
          this.el.html(this.templateB());
        }
        // now instantiate and render subviews
        this.graphView = new GraphView({
          // note that I'm using this.$ to help scope the element
          el: this.$('.graphContainer'), 
          model: graph
        });
        this.tableView = new TableView({
          el: this.$('.dataTableContainer'), 
          model: dataSet
        });
        // you probably need to call graphView.render()
        // and tableView.render(), unless you do that
        // in their initialize() functions
      },
    });
    

    Note that you don’t have to pass in an el at instantiation – you could instantiate subviews in initialize(), and set the el property in subview.render(). You could even instantiate in MainView.initialize(), as you do now, and pass the views to LayoutView to render asynchronously. But I think the above approach is probably cleaner.

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

Sidebar

Related Questions

I am currently running into a problem where an element is coming back from
I have a French site that I want to parse, but am running into
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 jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.