0

I have been trying for hours now, without luck. If you could see on the image, I have mildly complex model. Image is taken from Chrome in debug.

I need to delete a model from collection, also I need to be able to change the URL where the backbone will shoot its ajax for delete. So in essence, this is my model structure:

attributes: favorites { bookmarkedArticles: [{id: 123123},{id: ...}], bookedmarkedSearches: [{}], dispatchesMailds: [] } 

How can I delete model in bookmarkedArticles with id of 123123?

I have tried this:

 var model = new metaModel( { favourites: { bookmarkedArticles: { id: "123123" } } } ); model.destroy(); 

also this

aamodel.headerData.collection.remove(model); 

No success at all.

Model image from Chrome Debug

1
  • 1
    No success at all. ... what does that exactly mean? Any error message? Which URL is called upon destroy? Please be a bit more specific. Commented Apr 30, 2015 at 13:45

1 Answer 1

1

The information provided is not giving a lot of details, but I will try to answer considering two scenarios:

Option A: You are trying to delete a model in the collection that has bookmarkedArticle.id="123123". if that is the case and considering the bookmarkedArticles it is just an Array of objects, I would suggest to filter the Collection using the underscore method filter and then delete the models returned by the filter.

var id = 123123; var modelsToDelete = aamodel.headerData.collection.filter(function(model){ // find in the bookmarked articles return _.find(model.get('bookmarkedArticles'), function(ba){ return (ba.id === id); }); }); _.each(modelsToDelete, function(model){ model.destroy(); }); 

Option 2: If you want to remove the bookmarked article '123123' associated to your main model using just the 'destroy' method, firstable you have to convert 'bookmarkedArticles' to a Backbone.Collection as it is just an Array of Objects, there are some utilities for Backbone that allows you to do this easily:

https://github.com/blittle/backbone-nested-models

But by default this is not possible, then, If you want to remove the 'bookmarkedArticle' you can create the Backbone.Model and then use the method destroy. Example:

var BookmarkedArticle = Backbone.Model.extend({ url: function(){ return '/bookmarkArticle/' + this.id; } }); new BookmarkedArticle({"id": "123123","master": "5",...}).destroy(); 

Hope this information is useful and provide some guidance to solve your problem.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.