【问题标题】:Model is not deleted from backbone collection模型未从主干集合中删除
【发布时间】:2013-10-02 17:00:28
【问题描述】:

我仍在尝试从我的收藏中移除(销毁)模型。数据被分组并呈现为手风琴风格。但是当我在控制台中点击 X 时,会注意到:

Uncaught Uncaught TypeError: Cannot call method 'destroy' of undefined  


   (function() {
    window.App = {
        Models: {},
        Collections: {},
        Views: {},
        Router: {}
    };
    window.vent = _.extend({}, Backbone.Events);
})();

// !models.js

App.Models.Item = Backbone.Model.extend({});

// !collections.js

App.Collections.Items = Backbone.Collection.extend({
    model: App.Models.Item,
    url: 'api/items.json'
});

// !views.js

App.Views.Items = Backbone.View.extend({

    el: '#items',
    events: {
      'click .cccc':'deleteItem',
    },
    deleteItem: function() {
      this.model.destroy();  
    },
    initialize: function() {
     this.listenTo( this.collection, "change", this.render );
     this.template = _.template( document.getElementById('productsCategoriesTemlate').innerHTML );
     this.render();
     this.$el.accordion({ animate: 0 });
    },
    getGroups : function(){
       return _.groupBy(this.collection.toJSON(), 'category');
    },
    render: function() {
        this.el.innerHTML = this.template({ data : this.getGroups() });

    },

    addOne: function(item) {
       // ????????????
    }
});

App.Views.Item = Backbone.View.extend({
      deleteItem: function() {
      this.model.destroy();  
      },

      // ???????????
 });

// !router.js

App.Router = Backbone.Router.extend({
    routes: {
        '':'index',
    },
    index: function() {
        console.log('index page !');
    },
});

new App.Router;
Backbone.history.start();

App.items = new App.Collections.Items;
App.items.fetch().then(function() {
    new App.Views.Items({ collection: App.items });
});

模板:

<script id="productsCategoriesTemlate" type="text/template">
    <% _.each( data, function( category, i ){  %>
        <h3 class="category-name"><%= i %></h3>
        <div><% _.each( category, function( item ){ %>
            <li class="product"><%= item.title %><p style="float:right;" class="cccc">X</p></li>
            <% }) %>
        </div>
   <% }) %>
</script>

【问题讨论】:

  • 请展示您的收藏和型号
  • 主干集合没有方法destroy。您在寻找remove 吗?如果是这样,您需要传递要删除的模型。
  • 谢谢大家的回答!我已经编辑了代码...

标签: javascript backbone.js underscore.js


【解决方案1】:

你在哪里实例化Apps.Views.Items?这是你的“收藏观”吗?如果此视图代表您的集合,您必须以某种方式传递或引用“deleteItem”上的模型。

App.Views.Items 不代表单个模型,所以this.model 不正确。

更新

您应该为每个项目(例如 App.Views.Item)有一个单独的视图,并为 App.Views.Items 集合中的每个模型循环并创建此视图。

第二次更新

是的,你明白了。这是我拼凑的一些示例代码(我没有测试它,所以你可能需要调整它,但它提供了一个好主意。模板渲染语法可能不正确,因为我通常不手动执行)。

App.Views.Items = Backbone.View.extend({
  render: function() {
    this.collection.each(function(model) {
      var view = new App.Views.Item({ model: model });
      this.$el.append(view.render().el);
    });
  },
});

App.Views.Item = Backbone.View.extend({
  template: _.template($('#itemViewTemplate')),
  render: function() {
    this.$el.html(this.template(this.model.toJSON()));
  },
});

App.items = new App.Collections.Items;
App.items.fetch().then(function() {
    var items = new App.Views.Items({ collection: App.items });
    $('body').append(items.render().$el);
});

顺便说一句,一旦您掌握了 Backbone 的窍门以及它的工作原理,您应该尝试一下 Marionette.js。它使所有这类事情变得更加简单。

【讨论】:

  • 但是我需要这个集合或模型分组......我怎样才能做到这一点?非常感谢
  • 我之前有过类似你告诉我的事情,但我不知道如何在那里实现 groupby 功能:stackoverflow.com/questions/18993798/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-30
  • 1970-01-01
  • 2014-12-09
  • 2023-03-06
  • 2015-08-31
相关资源
最近更新 更多