如何删除骨干模型

发布于 2024-12-21 06:38:04 字数 2342 浏览 0 评论 0原文

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

我收到“模型未定义错误”。

不知道为什么找不到型号?我缺少什么参数?我在这个列表视图中添加这个是错误的吗?我应该将其添加到模型视图中吗?如果是这种情况,我在列表视图中添加了保存新模型,所以不明白为什么我不能将其添加到她。

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







    });

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







    });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绝影如岚 2024-12-28 06:38:04

您必须将clearCompleted方法绑定到this

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

You have to bind your clearCompleted method to this:

_.bindAll(this, 'render', 'clearCompleted');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文