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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:01:20+00:00 2026-06-18T12:01:20+00:00

I have a model which has both navid and subnavid .While destroying a model

  • 0

I have a model which has both navid and subnavid .While destroying a model i need to check in the entire collection , for other models which have navid as same as subnavid of the model i’am trying to delete . Please help me out . Thanks in advance . Heregoes my sample code.

Model:

var Node = Backbone.Model.extend({
defaults: {
NavId: ”,
SubNavId: ”.
ItemName:”
} }

Collection:

var NodeCollection = Backbone.Collection.extend({ model:Node }

And i have two view one for the Node(i am building tr) and other for
the collection(I need to build table) var NodeCollectionView =
Backbone.View.extend({

initialize: function (options) {
    var self = this;      self.collection = new NodeCollection({ NavigationId: options.NavigationId });
    self.collection.fetch({
        success: function () {
            /*I am getting  hte proper collection from my restful api and iam able to bind it properly
          self.render();
        }
    });
},

render: function () {
    var that = this;
    _.each(this.collection.models, function (item) {
        that.RenderEachNode(item);
    }, this);
},

RenderEachNode: function (item) {
    var TempJsonNode = item.toJSON();
    var self = this;
    var nodeView = new NodeView({
        tagName: 'tr',
        id: 'NavId_' + TempJsonNode.NavItemId,
        model: item
    });
} });
var ItemTemplate = ""; ItemTemplate += "    <td>"; ItemTemplate += "        <a><%= ItemName %></a>"; ItemTemplate +="   </td>"; ItemTemplate
+=" <td>"; ItemTemplate +="         <a href='#' original-title='Delete ' class='tip_north Delete'>X</a>"; ItemTemplate +="  </td>  ";           


var NavigationItemView = Backbone.View.extend({
    template: ItemTemplate,
    render: function () {
        var self = this;
        var tmpl = _.template(this.template);
        this.$el.html(tmpl(this.model.toJSON()));
        return this;
    },
    events: {
        "click .Delete": "DeleteBtnClick"
    },
    DeleteBtnClick: function () {
        var self = this;
        self.model.destroy({
            success: function (status, data) {
                var RetData = JSON.parse(data);
                if (RetData.Status == 'Success') {
                    $(self.el).remove()
                }
            },
            error: function () {
                alert('Error In Deleting The Record');
            }
        });
        return false;
    } });

I am able to build the table properly but while destroying a model , i am not figuring out a way to destroy the dependent models.My Api is restricted in such a way that i cannot get a nested json ( if so i would have done with backbone relation). so i need to figure out some way that the other models and views which has the NavId of the model am deleting.

Please help me out.

  • 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-18T12:01:21+00:00Added an answer on June 18, 2026 at 12:01 pm

    How about something like:

    var NodeView = Backbone.View.extend({
      initialize: function() {
        //when the model gets destroyed, remove the view
        this.listenTo(this.model, 'destroy', this.remove);
      },
      //..clip
      DeleteBtnClick: function () {
        var self = this;
        var collection = self.model.collection;
        var navId = self.model.get('NavId');
    
        self.model.destroy({
          success: function (status, data) {
            var RetData = JSON.parse(data);
            if (RetData.Status == 'Success') {
              //if parent was part of a collection
              if (collection) {
                //find related models
                var related = collection.filter(function (model) {
                  return model.get('SubNavId') === navId;
                });
    
                //call destroy for each related model.
                var promises = _.invoke(related, 'destroy');
    
                //optional: if you want to do something when all the children
                //are destroyed:
                $.when.apply($, promises).then(function () {
                  console.log('all destroyed');
                });
              }
            }
          },
          error: function () {
            console.log(arguments);
            alert('Error In Deleting The Record');
          }
        });
        return false;
      }
    });
    

    Edit: JSFiddle here

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

Sidebar

Related Questions

I have a Model which has some constants defined, like below: class Order(models.Model): WAITING
I have model Foo which has field bar. The bar field should be unique,
I have a model which has a certain behaviour implemented. class X { ....
I'm using ASP.NET MVC and have a model which has an image (byte array)
I have an Entry model which has many Tag s. Tag s are added
I have a Django model which has a foreign key that is optional. I
I have an interesting scenario. I have a task model which has a task
I have got an Entity model which contains a collection of Message objects which
So I have a view model call ProductViewModel which has a list of sites
I have a controller, Hardware which has a Create action. The model looks like:

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.