【问题标题】:how to delete a backbone model如何删除主干模型
【发布时间】:2011-12-14 09:59:11
【问题描述】:

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

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

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

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







    });

【问题讨论】:

  • 你如何实例化这个视图?
  • 您确定您的视图确实具有模型属性吗?我在您的视图定义中没有看到任何其他对它的引用。

标签: javascript backbone.js


【解决方案1】:

你必须将你的 clearCompleted 方法绑定到this:

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

【讨论】:

  • 不,你没有。 delegateEvents 在实际将其交给 jquery 之前,实际上为您将所有事件绑定到“view's this”。来自文档:“”“与手动使用 jQuery 在渲染期间将事件绑定到子元素相比,使用 delegateEvents 提供了许多优势。所有附加的回调在传递给 jQuery 之前都绑定到视图,因此当调用回调时,这继续引用视图对象。"""
猜你喜欢
  • 1970-01-01
  • 2013-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-27
  • 1970-01-01
相关资源
最近更新 更多