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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:33:46+00:00 2026-05-31T04:33:46+00:00

Have som trouble getting the code below to work. I am trying to fire

  • 0

Have som trouble getting the code below to work.

I am trying to fire an event from a rendered sub view that has its own events object.

Is it possible to do this in an easy way?

var SubView = Backbone.View.extend({
    events: {
      'click .subview-item a': 'test'
    },
    el: '#subview',
    test: function() {
      console.log('please print this...');
    },
    initialize: function() {
      this.template = '<div class="subview-item"><a href="#">Clickable Subview</a></div>'
    },
    render: function(){
      $(this.el).html(_.template(this.template));
      return this;
    }
});

var MainView = Backbone.View.extend({
    el: $('#content'),
    initialize: function(){
      this.template = '<h1>Hello</h1><div id="subview"></div>';
      this.subView = new SubView();
    },
    render: function(){
      $(this.el).html(_.template(this.template));
      this.subView.render();
      return this;
    }
});

var mainView = new MainView({});
mainView.render();

Any ideas??

  • 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-31T04:33:48+00:00Added an answer on May 31, 2026 at 4:33 am

    When you create your subView inside MainView‘s initialize, the #subview element does not yet exist in your DOM, since you have not yet rendered MainView. So a new <div> is created outside the DOM. You need to first render MainView before creating the SubView. You could do that inside the MainView‘s render() but the following is simpler I think:

    var SubView = Backbone.View.extend({
        events: {
            'click .subview-item a': 'test'
        },
        el: '#subview',
        test: function() {
            console.log('please print this...');
        },
        initialize: function() {
            this.template = _.template('<div class="subview-item"><a href="#">Clickable Subview</a></div>');
        },
        render: function() {
            this.$el.html(this.template);
            return this;
        }
    });
    
    var MainView = Backbone.View.extend({
        el: $('#content'),
        initialize: function() {
            this.template = _.template('<h1>Hello</h1><div id="subview"></div>');
        },
        render: function() {
            this.$el.html(this.template);
            return this;
        }
    });
    
    var mainView = new MainView();
    mainView.render();
    var subView = new SubView();
    subView.render();
    

    Also took the liberty to correct a few things like using this.$el and creating the template on initialize() rather than recompiling on every render().

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

Sidebar

Related Questions

I have a very long price file from my wholesaler that I have som
I have some code that will trigger Code Analysis warning CA1063: CA1063 : Microsoft.Design
I have noticed that, from Google Maps page, you can get an embed link
I'm revisiting som old code of mine and have stumbled upon a method for
I have a game in Android that force closes after resuming from a phone
I have som text-content that I want to be able to change without recompiling
I got som difficulties to output HTML-string code through XML.I have presented a example
I have created a fulltext catalog that stores the data from some of the
Have a SomeLib.pro file that contains: CONFIG += debug TEMPLATE = lib TARGET =
Have someone tried out DeCAL in Delphi 2009? I'm thinking about upgrading from 2007,

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.