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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:30:13+00:00 2026-05-23T09:30:13+00:00

Using backbone.js: I have a top level ModelA that contains 2 attributes and 2

  • 0

Using backbone.js:

I have a top level ModelA that contains 2 attributes and 2 nested models, ModelB and ModelC. ModelB and ModelC each have 2 attributes as follows:

ModelA
    attributeA1
    attributeA2
    ModelB
        attributeB1
        attributeB2
    ModelC
        attributeC1
        attributeC2

There is a ViewA for ModelA, and a ViewB for ModelB.
ViewA’s render function places a new div onto the body, whereas ViewB’s render creates an h1. ViewA’s initialization calls ViewB’s render to insert that h1 into the new div. The rationale behind this separation is that the h1 may change and require re-rendering independent from ViewA.

ViewA
    initialise: 
        //call ViewA's own render function
        this.render() 

        //call ViewB's render function that further modifies the $("#new") div created earlier.
        $("#new").append(ViewB.render().el)

    //ViewA's own render function
    render: //place <div id="new"></div> onto 'body'

ViewB
    render: //create a <h1></h1>
    funcB1: //can this access it's parent ModelA's attributes and other objects?

Q1: ViewB has a function funcB1. Can this function access it’s parent model’s attributes? Attributes such as attributeA1, or even attributeC1 (which would be a sibling/cousin)?

Q2: As a further expansion to Q1, can funcB1 access the DOM elements associated with ViewA? (in this example, the #new div?)

Q3: In general, how do I define the associations between the Views and Models as described above so that everything ties together properly?

I realize this question is somewhat abstract but any appreciate any help or guidelines appreciated.

  • 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-23T09:30:14+00:00Added an answer on May 23, 2026 at 9:30 am

    To be able to reach attributes on related models, the model must have some kind of knowledge about what models it is related to. Backbone.js does not implicitly deal with relations or nesting, which means you must yourself make sure that the models have knowledge of each other. To answer your questions, one way to go about it is to make sure each child model has a ‘parent’ attribute. This way you can traverse the nesting first up to the parent and then down to any siblings that you know of.

    To be more specific with your questions. When initializing modelA, you are probably creating modelB and modelC, I would suggest setting a link to the parent model when doing this, like this:

    ModelA = Backbone.Model.extend({
    
        initialize: function(){
            this.modelB = new modelB();
            this.modelB.parent = this;
            this.modelC = new modelC();
            this.modelC.parent = this;
        }
    }
    

    This way you can reach the parent model in any child model function by calling this.parent.

    Regarding your views, when doing nested backbone views, I find it easier to let each view represent one HTML tag by using the tagName option of the view. I would write your views as this:

    ViewA = Backbone.View.extend({
    
        tagName: "div",
        id: "new",
    
        initialize: function(){
           this.viewB = new ViewB();
           this.viewB.parentView = this;
           $(this.el).append(this.viewB.el);
        }
    });
    
    ViewB = Backbone.View.extend({
    
        tagName: "h1",
    
        render: function(){
            $(this.el).html("Header text"); // or use this.options.headerText or equivalent
        },
    
        funcB1: function(){
            this.model.parent.doSomethingOnParent();
            this.model.parent.modelC.doSomethingOnSibling();
            $(this.parentView.el).shakeViolently();
        }
    
    });
    

    Then in your application initialization code (eg in your controller), I would initiate ViewA and place its element inside the body element.

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

Sidebar

Related Questions

I'm using backbone js with an xml api data feed. I have a top-level
We are using Backbone.js to build client-side UI that manipulates models shared by multiple
I'm building an app using Brunch and Backbone.js that is to include nested menus.
I am using underscore.js's templating capabilities from backbone.js, I have the following template that
I'm using Backbone.js have a segmented control-type UI element for each model's view. They
I'm using codebrew\backbone-rails in a nested model example (say I have a collection of
Using Backbone.js what is the best pattern to have models listen to all of
Using TortoiseSVN against VisualSVN I delete a source file that I should not have
I'm using backbone's underscore templating engine with the mustache formatting patterns. I have already
If I have done my homework correctly, I have come to learn that Backbone

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.