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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:56:58+00:00 2026-05-26T05:56:58+00:00

I’m getting nuts with this thing. I really can’t figure out what’s the issue

  • 0

I’m getting nuts with this thing. I really can’t figure out what’s the issue here.

I have a collection of models called projects, and each of this project has its own projectviewitem.

The task I’m trying to perform is just deleting all models belonging to the collection. The corresponding model views should also be removed along the way.
Here is how I do it:

empty: function() {
    for(i=0; i<this.length; i++) {
        var model = this.at(i);
        console.log("script remove: "+model.get('id'));
        model.destroy();
    };

I’m making sure I’m destroying the right models with the console.log output which is giving me each model id.

In the project model I have this destroy function:

destroy: function() {
    console.log(this);
    console.log("model remove: "+this.get('id'));
    return Backbone.Model.prototype.destroy.call(this);
    },

Again I’m making sure I deal with the proper model.
I also inspect the structure of the model to be sure the callback is right.

The projectviewitem is initialized within the model’s initialize method:

initialize: function() {
    this.view = new ProjectViewItem({model: this});
},

It is bound to the model:

initialize: function() {
    this.model.bind('destroy', this.remove, this);
    ...

And its destroy method is:

remove: function() {
    console.log("remove: "+this.model.get('id'));
    return Backbone.Model.prototype.remove.call(this);
},

About the console.log output now.

Here is what I get when emptying the collection.

script remove: 344
d (model 344 object)
model remove: 344
//here I should get "remove: 340", but I don't. That means the projectviewitem's remove method isn't called, but why?
script remove: 343
d (model 343 object)
model remove: 343
remove: 343
remove: 343
//model 343 makes two callback calls?

As a result, all models are deleted but only one view is. I don’t understand how model 344 loses the callback to its view and how model 343 gets two..
When I check out the objects (>> d), the callbacks are alright. 344 has its own, same for 343, and they seem to be pointing to their corresponding view.

Any clue?

Until I solve this issue I add (this.view.remove();) to the model.destroy method. It works but I would definitely like to know why the binding is not working properly.

  • 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-26T05:56:58+00:00Added an answer on May 26, 2026 at 5:56 am

    There are a few things that could be causing your problems here, though it’s hard to know for sure without more code:

    • You can’t use an incrementing for loop to delete items (destroy() will remove the item from the collection), because you’re changing the length as you go. The classic way to fix this is to iterate backwards, rather than forwards:

      empty: function() {
          for(i=this.length-1; i>=0; i--) {
              var model = this.at(i);
              console.log("script remove: "+model.cid);
              model.destroy();
          };
      }
      
    • The .destroy() method on a model with an id won’t trigger the destroy event unless there’s a successful response from the server (relevant code). So you need to make sure your server is responding with a 200 response to the DELETE request.

    • As noted in my comment, I’m not sure what you’re trying to do by calling Backbone.Model.prototype.destroy.call(this) on a view. I suspect this is a typo, either in your post or in your code, but I’m not sure what it would accomplish.

    I hope at least one of these issues is the source of your problem. I have a working jsFiddle with a slightly modified version of your code here: http://jsfiddle.net/e8Uct/3/ – it’s only using models created on the fly, though, so it’s not dealing with the server callback.

    As an aside, I personally don’t like the pattern of initializing the view with the model – I’d rather do it the other way around, and keep the model wholly independent of the UI. But that’s really a matter of taste.

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

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.