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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:15:18+00:00 2026-05-29T15:15:18+00:00

I have a backbone view that calls to a sub-view: lr.MapView = Backbone.View.extend({ el:

  • 0

I have a backbone view that calls to a sub-view:

  lr.MapView = Backbone.View.extend({
    el: $('#map'),
    foo: "bar",
    initialize: function() {
      var that = this;
      _.bindAll(this, "render", "addAllEvents", "addOneEvent");
      this.collection = new lr.Events();
      this.collection.fetch({
        success:  function(resp) {
          that.render();
          that.addAllEvents();
        }
      });   
    },

    addAllEvents: function() {
      this.collection.each(this.addOneEvent);
    },

    addOneEvent: function(e) {
      var ev = new lr.EventView({ 
        model:  e
      });
    },

    render: function() {
    } 
  });

Here is the sub-view:

  lr.EventView = Backbone.View.extend({
    initialize: function() {
      _.bindAll(this, "render");
      console.log(lr.MapView.foo); // will console.log 'undefined' 
    },
    render: function() {
    }
  });

I’d like to be able to access properties the parent view within the sub-view, but it isn’t working with the above code. For example, how can I access the ‘foo’ variable within the sub-view?

  • 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-29T15:15:19+00:00Added an answer on May 29, 2026 at 3:15 pm

    lr.MapView is a “class”, everything that Backbone.View.extend builds will be in lr.MapView.prototype, not in lr.MapView. Run this with the console open and you’ll see whats going on:

    var MapView = Backbone.View.extend({ foo: 'bar' });
    console.log(MapView);
    console.log(MapView.prototype);
    console.log(MapView.prototype.foo);
    

    Demo: http://jsfiddle.net/ambiguous/DnvR5/

    If you’re only going to have a single MapView then you can refer to lr.MapView.prototype.foo everywhere:

    initialize: function() {
      _.bindAll(this, "render");
      console.log(lr.MapView.prototype.foo);
    }
    

    Note that everywhere includes within lr.MapView instances so your foo will act like a “class variable” from non-prototype based OO languages.

    The right way to do this is to use an instance variable for foo and pass the parent view instance to the sub-view instances when they’re created:

    // In MapView
    addOneEvent: function(e) {
      var ev = new lr.EventView({
        model: e,
        parent: this
      });
    }
    
    // In EventView
    initialize: function(options) {
      _.bindAll(this, "render");
      this.parent = options.parent; // Or use this.options.parent everywhere.
      console.log(this.parent.foo); 
    }
    

    Or better, add an accessor method to MapView:

    _foo: 'bar',
    foo: function() { return this._foo }
    

    and use that method in EventView:

    initialize: function(options) {
        // ...
        console.log(this.parent.foo());
    }
    

    Proper encapsulation and interfaces are a good idea even in JavaScript.

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

Sidebar

Related Questions

I have this: var SomeNamespace = { Model: Backbone.Model.extend(), View: Backbone.View.extend({ model: new this.Model,
I have a backbone view like so window.InputView = Backbone.View.extend({ tagName:'input', className:'', attributes:{}, initialize:function(){
So here is my view: $(function() { var ImageManipulation = Backbone.View.extend({ el: $('body'), tagName:
The wierdest thing. I have a function which calls: var clicked_el = event.target.id; This
So I have a View that looks like this. //base class var SelectListView =
I have a backbone view - which when called presents a form $('#add-offer').click(function() {
I have a Backbone View for a general element that specific elements will be
TL;DR Is PinView.prototype = _.extend(PinView.prototype, google.maps.OverlayView.prototype) the proper way to have a Backbone View
I have a backbone app with a view structure that looks like the following
I have a Backbone View that uses iScroll to implement a slideshow. iScroll publishes

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.