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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:03:01+00:00 2026-05-31T14:03:01+00:00

I have a backbone collection and when I remove a model from the collection,

  • 0

I have a backbone collection and when I remove a model from the collection, I want it to remove the item from a list in the view.

My collection is pretty basic

MyApp.Collections.CurrentEvents = Backbone.Collection.extend({
    model: MyApp.Models.Event
});

and in my views I have

MyApp.Views.CurrentEventItem = Backbone.View.extend({
   el: 'div.current_event',

   initialize: function(){
        event = this.model;
        _.bindAll(this, "remove");
        MyApp.CurrentEvents.bind('remove',this.remove); //the collection holding events

     this.render();
  },

   // yeah, yeah, render stuff here

 remove: function(){
    console.log(this);
    $(this.el).unbind();
    $(this.el).remove();
  }
});

when I remove the model from the collection, it triggers the remove function, but the view is still on the page.
In the console, I can see the model, but I believe the model should have an ‘el’, but it doesn’t.

My container code is

MyApp.Views.CurrentEventsHolder = Backbone.View.extend({
    el: 'div#currentHolder',

    initialize: function(){
           MyApp.CurrentEvents = new MyApp.Collections.CurrentEvents();
           MyApp.CurrentEvents.bind('new', this.add);
   },
   add: function(){  
        var add_event = new MyApp.Views.CurrentEventItem(added_event);
        $('div#currentHolder').append(add_event.el);

     }
});

for some reason in the add method I can’t seem to use the $(this.el) before the append, though I’m not sure if that is the problem.

  • 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-31T14:03:04+00:00Added an answer on May 31, 2026 at 2:03 pm

    PROBLEM: MyApp.CurrentEvents.bind('remove',this.remove);

    This triggers the remove() function every time any model is deleted from the collection.

    This means that anytime a model is deleted, all the CurrentEventItem view instances will be deleted.

    Now, about the view still being on the page:

    It must have something to do with the way you appended/added/html-ed the view in the page. Check your other views, maybe if you have a CurrentEventsContainer view of some sort, check your code from there because with your current code, it does delete the view, albeit, all of them though.

    RECOMMENDED FIX:

    change your bindings to:

    this.model.bind('remove',this.remove);
    

    and make sure that when you instantiate it, pass on the model so that each view will have a corresponding model to it like so:

    //...
    addAllItem: function(){ 
          _.each(this.collection, this.addOneItem())
    },
    addOneItem: function(model){
          var currentEventItem = new MyApp.Views.CurrentEventItem({model:model});
          //...more code..
    }
    //...
    

    This makes things a lot easier to manage in my opinion.

    UPDATE (from your updated question)

    The reason you aren’t able to use this.el is because you haven’t passed the right context into the add() function. You need to add _.bindAll(this,'add') in your initialize() function to pass the right context, therefore making your this correct, allowing you to use this.el within the add function. Also, change your code to this:

    MyApp.CurrentEvents.bind('new', this.add, this); this passes the right context. Also, why not use add instead as an event?

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

Sidebar

Related Questions

I have a Backbone.js collection and want to delete few elements from it at
I have a view and collection like this: window.DmnView = Backbone.View.extend({ template: _.template($(#tmpl_dmnListItem).html()), events:
Newbie backbone question: Context: Building a shopping list with backbone I have a model
I want to render a view when a collection has been loaded asynchronously from
I have very simple backbone model and collection. I have a corresponding backbone.marionette.CollectionView and
I have the following Backbone.js model and collection: // Model lr.Event = Backbone.Model.extend({}); //
Here is what I have so far: var Item = Backbone.Model.extend({ defaults: { id:
let's say I have : var Book = Backbone.Model.extend(); var Collection = Backbone.Collection.extend({ model:
I have a Backbone.js Collection and I have an array of model IDs that
I'm using codebrew\backbone-rails in a nested model example (say I have a collection of

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.