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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:54:42+00:00 2026-06-14T17:54:42+00:00

I have a question about the way backbone handles it views. Suppose I have

  • 0

I have a question about the way backbone handles it views.
Suppose I have the following code:

<div id="container">
   <div id="header">
   </div>
</div>

After this I change header into a backbone view.

How can I now remove that view from the header div again after I’m done with the view and add ANOTHER view to the same div?

I tried just overwriting the variable the view was stored in. This results in the view being changed to the new one…but it will have all the event handlers of the old one still attached to it.

Thanks in advance!

  • 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-14T17:54:43+00:00Added an answer on June 14, 2026 at 5:54 pm

    http://documentcloud.github.com/backbone/#View-setElement

    This won’t automatically remove the original div – you’ll want to do that yourself somehow, but then by using setElement you’ll have the view’s element set to whatever you passed it.. and all of the events will be attached as appropriate. Then you’ll need to append that element wherever it is that it needs to go.

    — Let’s try this again —-

    So, first thing to keep in mind is that views reference DOM elements.. they aren’t super tightly bound. So, you can work directly with the jquery object under $el.

    var containerView = new ContainerView();
    
    var headerView = new HeaderView();
    var anotherHeaderView = new AnotherHeaderView();
    
    containerView.$el.append(headerView.$el);
    containerView.$el.append(anotherHeaderView.$el);
    
    anotherHeaderView.$el.detach();
    
    containerView.$el.prepend(anotherHeaderView.$el);
    

    Or you can create methods to control this for you.

    var ContainerView = Backbone.View.extend({
        addView: function (view) {
            var el = view;
            if(el.$el) { //so you can pass in both dom and backbone views
                el = el.$el;
            }
            this.$el.append(el);
        }
    });
    

    Maybe setting the views by view order?

    var ContainerView = Backbone.View.extend({
        initialize: function () {
            this.types = {};
        },
        addView: function (view, type) {
            var el = view;
            if(el.$el) { //so you can pass in both dom and backbone views
                el = el.$el;
            }
            this.types[type] = el;
            this.resetViews();
        },
        removeView: function (type) {
            delete this.types[type];
            this.resetViews();
        },
        resetViews: function () {
            this.$el.children().detach();
            _.each(['main_header', 'sub_header', 'sub_sub_header'], function (typekey) {
                if(this.types[typekey]) {
                    this.$el.append(this.types[typekey]);
                }
            }, this);
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question about the best way to go about the following I
I have a question about the way Django models the one-to-one-relationship. Suppose we have
I have a question about the correct way to switch views for an iPhone
I have a general question about the way that database indexing works, particularly in
I have a question about SqlDataReader: Is there some way to modificated the values
I have a question about whether or not a specific way of applying of
I have a question about the proper, best way to manage the model. I
I have a question about list in tcl, what is the correct way to
I have question about normalization. Suppose I have an applications dealing with songs. First
I have a question about the architecture of a backbone.js web app. Let's say

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.