user901790
user901790

Reputation: 319

how to delete a backbone model

I have created the following code and i'm unable to destroy a model from the backend.

I get a 'model is not defined error'.

I don't know why it cannot find a model? what parameters am i missing? am i wrong in adding this in this list view? should i add it in the models view? and if that is the case i added the save new model in the list view so can't see why i can't add it her.

   window.LibraryView = Backbone.View.extend({
            tagName: 'section',
            className: 'mynotes',


            events: {
                "keypress #new-title":  "createOnEnter",                    
                //"keypress #new-content":  "createOnEnter"
                "click .mybutton":  "clearCompleted"

            },


           initialize: function() {
                  _.bindAll(this, 'render');
                  this.template = _.template($('#library-template').html());
                  this.collection.bind('reset', this.render);
                  this.collection.bind('add', this.render);
                  this.collection.bind('remove', this.render);

           },

           render: function() {
                    var $mynotes,
                          collection = this.collection;

                    $(this.el).html(this.template({}));
                    $mynotes = this.$(".mynotes");
                    collection.each(function(mynote) {
                            var view = new LibraryMynoteview({
                                  model: mynote,
                                  collection: collection
                            });
                            $mynotes.append(view.render().el);

                    });
                    return this;
           },

            createOnEnter: function(e) {

                var input = this.$("#new-title");
                var input2 = this.$("#new-content");
                //var msg = this.model.isNew() ? 'Successfully created!' : "Saved!";
                 if (!input || e.keyCode != 13) return;
                // this.model.save({title: this.$("#new-title").val(), content:     this.$("#new-content").val() }, {
                var newNote = new Mynote({title: this.$("#new-title").val(), content: this.$("#new-content").val()});
                this.collection.create(newNote);

            },                                                                              
              clearCompleted: function() {
                       this.model.destroy();
                       this.collection.remove();
              }   







    });

Upvotes: 0

Views: 2861

Answers (1)

Andreas Köberle
Andreas Köberle

Reputation: 110892

You have to bind your clearCompleted method to this:

_.bindAll(this, 'render', 'clearCompleted');

Upvotes: 3

Related Questions