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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:13:05+00:00 2026-06-06T09:13:05+00:00

I am trying to build a switching view with backbone js and found out

  • 0

I am trying to build a switching view with backbone js and found out my bind event fired multiple times, when I switching view for few times.

Below is the code for better illustration:

html

<div id='container'>
    halo world
</div>

<button id='but1'>red view</button>
<button id='but2'>blue view</button>

css

#red_view{
    width:400px;
    height:400px;
    background-color:red;        
}
#blue_view{
    width:400px;
    height:400px;
    background-color:blue;        
}
.button,.button2{
    width:300px;
    height:300px;
    background-color:gray;
}

​
javascript

RedView = Backbone.View.extend({
    el: "#container",
    events:{"click .button":"clickme"},
    clickme:function(){
        alert('redview');                        
    },
    initialize: function(){
        this.$el.html("<div id='red_view'><div class='button'>Click Me</div></div>");            
    }
});

BlueView = Backbone.View.extend({
    el: "#container",
    events:{"click .button2":"clickme2"},
    clickme2:function(){
        alert('blueview');                        
    },    
    initialize: function(){
        this.$el.html("<div id='blue_view'><div class='button2'>Click Me</div></div>");            
    }
});

$(document).ready(function(){
    //var router = new SystemRouter();

    $('#but1').click(function(){
       var view = new RedView();
    });
    $('#but2').click(function(){
       var view = new BlueView();
    });    
});

If you click the red view for 3 times, and press ‘click me’. It will pops up alert for 3 times as well. I suspect there’s need to unbind the event somewhere, but couldn’t find proper way to do it. Best to provide some references of doing this correctly.

​Here’s the link to jsfiddle demo. http://jsfiddle.net/mochatony/DwRRk/31/

  • 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-06T09:13:06+00:00Added an answer on June 6, 2026 at 9:13 am

    Every time you click the red view or blue view -buttons you create a new Red or Blue View each and every time. You bind their events hash to respond to click DOM events originating from buttons with classes button and button2.

    1. Press ‘red view’ 3 times -> 3 instances of RedView created
    2. Click button with class ‘button’ -> DOM event
    3. 3 instances of RedView listening to said DOM event -> 3 alerts

    This is because you don’t clean the views before creating a new one effectively leaving you with ghost views that respond to events even though they can’t be seen. (More info on the events hash) You can clean the events from you views with something like this.

    cleanup: function() {
      this.undelegateEvents();
      $(this.el).clear();
    }
    

    Here is your fiddle with working cleanup of views http://jsfiddle.net/DwRRk/34/

    Also a hint for good practice: you should use something like a render method to attach stuff to your DOM, use the initialize to just initialize the needed values for your view.

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

Sidebar

Related Questions

I am trying build a jQuery EasyUI datagrid or treegrid out of a large
Trying to build out an exception if move.UserId does not equal currentUserId then Redirect
Trying to build openssl-fips-2.0 with NDK, before I was lucky found this link and
Trying to build Xuggler under Windows. Xuggler is core native code functions wrapped into
I found the MSDN example code for getting the default view of a collection
Trying to build a micro-CMS (of sorts), which needs to dish out content i.e.
Hello I'm trying to figure out why switching my compatability mode from 80 to
I am trying build an AST for the below grammer using ANTLR condition_in :
trying to build a query from a few tables here, and getting stumped on
I was trying Build For Archiving application (from Titanium Mobile) with xCode 4.4, but

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.