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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:06:26+00:00 2026-06-10T12:06:26+00:00

js. I would like to know why my backbone.js is removing both items when

  • 0

js. I would like to know why my backbone.js is removing both items when I click on the X next to the album and artist.

i.e.

The Chronic-- Dr. Dre-- X
Ten-- Pearl Jam-- X

I appreciate any feedback that I could get that would allow me to only remove one item as opposed to both

Javacript:

(function($){

Backbone.sync = function(method, model, success, error){ 
    success();
}

//Declare model 
var Album = Backbone.Model.extend({
defaults: {
    album: 'Logical Progresions vol. 1',
    artist:'LTJ Bukem'
    }
});

//Declare collection
var eList = Backbone.Collection.extend({
    model: Album
});


//Declare the view for the Albums
var AlbumView = Backbone.View.extend({
    el: $('div#main'),
    template: _.template(
            "<div class=\"alert\"> " +  
            "   <span class=\"album\"><%= album %>--</span> " +
            "   <span claas=\"artist\"><%= artist %>--</span> " +
            "   <span class =\"delete\">X</span> " +
            "</div>"
            ),


    events: { 
        'click span.delete':  'deleteAlbum'
    },  

    initialize: function(){
        _.bindAll(this, 'render','unrender','deleteAlbum');
        this.model.bind('remove', this.unrender);
    },

    // `unrender()`: Makes Model remove itself from the DOM.
    unrender: function(){
        this.$el.remove();
    },


    deleteAlbum: function(){
        this.model.destroy();
    },

    render: function(){
        $(this.el).append(this.template(this.model.toJSON()));
    }
});


var appendItem = function(item){
    var albumView = new AlbumView({
        model: item
        });
     albumView.render();
}

//// set the stuff in motion
var elist = new eList();

elist.bind("add",function(listItem){appendItem(listItem)});


elist.add({
    album: 'The Chronic',
    artist: 'Dr. Dre'
    });

elist.add({
    album: 'Ten',
    artist: 'Pearl Jam'
    });

})(jQuery);
  • 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-10T12:06:28+00:00Added an answer on June 10, 2026 at 12:06 pm

    There are a few things I can point out.

    First, your view – when you create multiple instances for each album – each share the same el. That is, div#main. Each time you add one, you’re appending the template stuff to the el which is why you still see the other. But when you click on .delete and execute the this.$el.remove() you are removing everything in the el. Which includes the other view.

    You should separate it out, each view should have it’s own unique el.

    el: 'div',
    className: 'albumView'
    

    When you add each album view, you can create the view and append it to your div#main

    var view = new AlbumView();
    $('#main').append(view.render().el);  // the el refers to the subview (albumView) el.
    

    This should keep each view happy with its own el and removal will only affect that view/model/DOMelement.

    UPDATE – context of $('#main').append(view.render().el);

    Basically, when you create the albumViews and append them the most ideal place to do this is in the larger context in which your div#main exists. For example, this might happen in your main js script in the header, or maybe even in a larger view that contains many albumView subviews. To illustrate the subview context:

    var ParentView = Backbone.View.extend({
        el: $('#main'),
        render: function() {
            this.addAllAlbums();  // On rendering the parent view, we add each album subview
            return this;
        },
        addAllAlbums: function() {
            var self = this;
    
            // This loops through the collection and makes a view for each album model
            this.collection.each(function(albumModel) {
                self.addAlbumView(albumModel);
            });
        },
        addAlbumView: function(albumModel) {
            var view = new AlbumView({
                'model': albumModel
            });
    
            // We are adding/appending each albumView to this view's el
            this.$el.append(view.render().el);
    
            // ABOVE: `this.$el` refers to ParentView el. view.render().el refers to the
            // albumView or subview el.
    
            // As explained before, now each album view has it's own el which exists in
            // the parent view's this.el = `$('#main')`
        }
    });
    
    
    // We create the parent BIG/ALLAlbumsView and toss into it the collection of albums
    var BigAlbumsView = new ParentView({
        'collection': albumsCollection
    });
    
    BigAlbumsView.render();  // Run the `render()` to generate all your album subviews
    

    You might also want to store reference to those subviews by adding these lines in your code of the parent view. It will make cleaning up easier although it’s not a big deal if you intend on cleaning up individual views through the subviews themselves.

    // In your initialization, we create an array to store album subviews
    this.albumViews = [];
    
    // In `addAlbumView()` we push each view into the array so we have a reference
    this.albumViews.push(view);
    
    // When cleaning up, you just simply cycle through the subviews[] and remove/close
    // each album subview
    _.each(this.albumViews, function(albumView) {
        albumView.$el.remove();
    });
    

    Hope this helps.

    PS – Last note that I noticed. When you remove a view, I noticed you use remove() which is the way to get it out of the DOM. If you’re making more complex subviews intertwined/tangled with event listeners to collections, models, and other views – you might want to read Derick Bailey’s take on Zombie views and implementing a close() method that will both remove() and unbind() your view so there are no references to it and it can be garbage collected. Not the focus of this question but good for extra credit and possibly relevant since this has probably made your code more complicated. 😛

    Removing Views – avoiding zombies

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

Sidebar

Related Questions

Currently I am using backbone-rails in my Rails app. I would like to know
I would like to know what is the recommaned way of nesting Backbone Views.
im doing a form with Backbone-Form.js and i would like to know if are
i would like know some reference. I know i can googling it. but prefer
Would like to know what a programmer should know to become a good at
Would like to know the c# code to actually retrieve the IP type: Static
Would like to know how to integarate cruise control with maven? Cruise Control comes
Would like to know how to hide an div after a set of css3
I would like to know what code to use to convert a double[] array
I would like to know, how can we change the SRC url for IFRAME

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.