如何删除骨干模型

时间:2011-12-14 09:59:11

标签: javascript backbone.js

我创建了以下代码,但我无法从后端销毁模型。

我得到的模型未定义错误'。

我不知道为什么找不到模特?我错过了哪些参数?我在这个列表视图中添加这个是错误的吗?我应该在模型视图中添加它吗?如果是这种情况,我在列表视图中添加了保存新模型,因此无法理解为什么我无法将其添加到她。

   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();
              }   







    });

1 个答案:

答案 0 :(得分:3)

您必须将clearCompleted方法绑定到this

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