【问题标题】:Calling destroy on collection在收集时调用销毁
【发布时间】:2013-05-13 07:26:25
【问题描述】:

我正在做一个类似于 Backbone-Todo 的示例应用程序。但是当我在集合上调用破坏时,它会给出错误:

未捕获的类型错误:无法读取未定义的属性“销毁”

我该如何解决这个问题。请提出建议。

以下是我的方法代码:

$(function(){

var Todo = Backbone.Model.extend({

defaults: function() {
  return {
    title: "empty todo...",
    order: Todos.nextOrder(),
    done: false
  };
}

});


var TodoList = Backbone.Collection.extend({

  model : Todo,

  localStorage: new Backbone.LocalStorage("todos-backbone"),

  done: function() {
    return this.where({done: true});
  },

  remaining: function() {
    return this.without.apply(this, this.done());
  },

  nextOrder: function() {
    if (!this.length) return 1;
    return this.last().get('order') + 1;
  },

  comparator: 'order'   
});

var TodoView = Backbone.View.extend({

  tagName:  "li",

  template: _.template($('#item-template').html()),

  events: {
    "click a.destroy" : "clear"
  },

  initialize: function() {
    this.listenTo(this.model, 'destroy', this.remove);
  },

  render: function() {
    this.$el.html(this.template(this.model.toJSON()));
    return this;
  },

  clear: function(){
    this.model.destroy();
  }
});

var AppView = Backbone.View.extend({

  el: $("#todoapp"),

  statsTemplate: _.template($('#stats-template').html()),

  events: {
    "keypress #new-todo":  "createOnEnter",
    "click #remove-all": "clearCompleted"
  },

  initialize: function() {
    this.input = this.$("#new-todo");
    this.main = $('#main');
    this.footer = this.$('footer');

    this.listenTo(Todos, 'add', this.addOne);
    this.listenTo(Todos, 'all', this.render);

    Todos.fetch();
  },

  render: function() {
    var done = Todos.done().length;
    var remaining = Todos.remaining().length;

    if (Todos.length) {
      this.main.show();
      this.footer.show();
      this.footer.html(this.statsTemplate({done: done, remaining: remaining}));
    } else {
      this.main.hide();
      this.footer.hide();
    }
  },

  createOnEnter: function(e){
    if(e.keyCode != 13) return;
    if (!this.input.val()) return;
    Todos.create({
      title: this.input.val()
    })  
    this.input.val('');         
  },

  addOne: function(todo){
    var view = new TodoView({model: todo});
    this.$("#todo-list").append(view.render().el);
  },

  clearCompleted: function(){
    _.invoke(Todos, 'destroy');
    return false;
  }

});

【问题讨论】:

  • 您的问题可能需要更多上下文。原始代码调用“Todos.done()”上的方法,该方法延迟目标对象可用时的调用(在好的情况下)。
  • 你好 Eric,我已经提供了我的应用程序代码。你现在能想出来吗。谢谢

标签: backbone.js


【解决方案1】:

对于这个答案,我假设TodosTodoList 的一个实例。我还假设您的错误是由AppView中的此函数触发的@

clearCompleted: function(){
  _.invoke(Todos, 'destroy');
  return false;
}

在那里,您试图将您的 Backbone.js Collection 实例视为它的本来面目,一个集合,例如一个列表。但是 Backbone 集合不仅仅是列表,它们是具有属性 models 的对象,这是一个包含所有模型的列表。所以尝试在对象上使用下划线的invoke(which works on lists),势必会导致错误。

不过不用担心,Backbone 为其ModelCollectionincluding invoke 巧妙地实现了许多Underscore 方法。这意味着您可以像这样为集合中的每个模型调用destroy

SomeCollection.invoke('destroy');

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-28
    • 2011-11-27
    • 1970-01-01
    • 2012-04-20
    • 1970-01-01
    • 2012-01-30
    • 1970-01-01
    • 2016-12-06
    相关资源
    最近更新 更多