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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:45:27+00:00 2026-05-24T13:45:27+00:00

I’m having some trouble deleting an item from a collection inside a model when

  • 0

I’m having some trouble deleting an item from a collection inside a model when in a view. Basically the model/collection structure is the following:

enter image description here

Basically when I try to remove an item from the sub item collection in the sub item view it actually removes the correct item from the collection. However when I come to persisting the main model the item seems to be still in the collection.
This is the how my views are structured:

enter image description here

The main view inserts the DOM nodes required by the main model, and them main model creates a new view for the item model etc. All views are getting the main model as model option like so:

new App.Views.MainModelView({
  model : this.model,
  el : $('#nodeID')
})

The only difference is in the creation of the Sub-item model view, where, due to re usability of the view and template, I still pass in the main model, however I also pass in the item in the item collection that is currently being modified. Which looks like this:

new App.Views.ItemView({
  model : this.model,
  item : this.selectedItem,
  el : $('#nodeID')
});

In the sub-item’s view init method I do the following:

this.item = (this.options.item) ? this.options.item : this.model;

To remove a sub-item from the sub-item collection I do:

removeSubItem : function(e) {
  // get the id of the sub-item to be removed
  var id = $(e.target).closest('tr').attr('data-id');
  if (!id) throw  "Could not retrieve data id";
  // retrieve the sub-item from the collection
  var subItem = this.item.subItems.get(id);
  // remove the sub-item from the collection
  this.item.subItems.remove(subItem);
},

As I said earlier when I remove the sub-item and I inspect the collection modified by the view I can see that the sub-item has been removed from the collection, however then I persist the main model the removed sub-item re-appears. The leads me to believe that somewhere along the line the sub-item collection might be cloned which could explain the sudden reappearance of the sub-item.

I know this is a fairly specific problem and I’m not sure if it is possible to get to the cause of the problem with what I have provided here, if you need any more information please let me know.

Thanks for all your help,

Vincent

========== EDIT ============

To answer some of the questions below let me outline the scope in which I am experiencing this issue:

If I console log the this.item.subItems collection in the SubItem view, after removeSubItem was called, I can see that the instance of the SubItem model has been removed successfully.
Before I call the save method on the main model I console log the return of the toJSON function. At this point I am experiencing the problem that the previously removed instance is ‘back’ in the collection. I have been monitoring the traffic between client and server with both Wireshark and Google chrome’s developer console and there is no call to the server to refresh any of the models.

The toJSON method for the SubItem collection looks like this:

toJSON : function() {
  App.log(["SubItem::collection::toJSON", this], "info");
  var json = {};
  // make sure the key for each SubItem is the primary key
  this.each(function(subItem) {
    json[subItem.get('id')] = subItem.toJSON();
  });

  return json;
}
  • 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-24T13:45:27+00:00Added an answer on May 24, 2026 at 1:45 pm

    Backbone.js support for nested collection/models is non-existent, and they provide no saving support (see http://documentcloud.github.com/backbone/#FAQ-nested). You have to override toJSON on any model with a subcollection. I’ve run into this scenario a million times. If you have something like (in coffeescript):

    class MainModel extends Backbone.Model
    
        itemCollection: ->
            @_itemCollection ?= new ItemCollection(@get('itemCollection'))
    
    
    class ItemCollection extends Backbone.Collection
    
        model: ItemModel
    
    
    class ItemModel extends Backbone.Model
    
        subCollection: ->
            @_subCollection ?= new SubCollection(@get('subCollection'))
    
    
    class SubCollection extends Backbone.Collection
    
        model: SubModel
    
    
    class SubModel extends Backbone.Model
    
    
    mainModel = new MainModel(json)
    

    Then in order for mainModel.save() to work, you need to override toJSON on MainModel and ItemModel, like:

    class MainModel extends Backbone.Model
    
        itemCollection: ->
            @_itemCollection ?= new ItemCollection(@get('itemCollection'))
    
        toJSON: ->
            return _.extend(@attributes, {itemCollection: @itemCollection().toJSON()})
    
    
    class ItemModel extends Backbone.Model
    
        subCollection: ->
            @_subCollection ?= new SubCollection(@get('subCollection'))
    
        toJSON: ->
            return _.extend(@attributes, {subCollection: @subCollection().toJSON()})
    

    I wrote the example in coffeescript because it’s much more concise than javascript. If you need any help making sense of it, please just ask.

    Hope this helps!

    — Note —

    Technically, in coffeescript, the toJSON methods could simply be:

    toJSON: ->
        _.extend @attributes, itemCollection: @itemCollection().toJSON()
    

    But I wrote it the way I did to be more understandable to non-coffeescripters.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have just tried to save a simple *.rtf file with some websites and
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have some data like this: 1 2 3 4 5 9 2 6
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... 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.