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 Answers1

2

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

jevakallio
  • 35,324
  • 3
  • 105
  • 112
  • Hi Fencliff iam new to backbone js i will prepare a quik fiddle can you please modify thatand show me? – Arun Kumar Feb 06 '13 at 13:45
  • @ArunKumar, I can take a look. If I can do it in short amount of time, I will. – jevakallio Feb 06 '13 at 14:04
  • @ArunKumar, JSFiddle is being really slow. I'll take a look at this later, when I have time. – jevakallio Feb 06 '13 at 14:59
  • @ArunKumar, added a fiddle in the post and updated the code. Hope this helps. – jevakallio Feb 06 '13 at 15:20
  • iam not able to find any changes in the fiddle – Arun Kumar Feb 06 '13 at 15:27
  • @ArunKumar, check the link in my answer. – jevakallio Feb 06 '13 at 15:36
  • thanks for the code provided . but one small glitch is there in the code for example electronics is of root if i remove the electronics then all the tr should delte . but that is not working it is working only upto one level. – Arun Kumar Feb 06 '13 at 15:44
  • @ArunKumar, oh. I'm no longer in front of my computer, and won't be for a while. You need to move the removal logic to the model level, methinks. – jevakallio Feb 06 '13 at 15:47
  • ok i will try that . thank you very much for the help provided . if iam not able to do that i will get back to you . thankyou – Arun Kumar Feb 06 '13 at 15:49
  • Hi i tried what you told me and now iam able to generate the list and delte as i expected . Thanks for the help .. Now i have other require ment where the bindind of tr will have a sequnce .. based on sequnce i need to find the tr for which i have to append the newly genarated tr html... – Arun Kumar Feb 08 '13 at 07:48
  • @ArunKumar, maybe open another question about that? And please mark this one accepted, if it works. – jevakallio Feb 08 '13 at 07:55
  • plza check this http://stackoverflow.com/questions/14769378/how-to-make-a-dynamic-el-in-backbone-biew – Arun Kumar Feb 08 '13 at 09:26