2013-05-13 66 views
0

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

Uncaught TypeError: Cannot read property 'destroy' of undefined

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

以下是我的方法的代码:

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

});

+0

您的问题可能需要更多的上下文。原始代码调用“Todos.done()”方法,该方法在目标对象可用时推迟调用(在正确的情况下)。 – 2013-05-13 07:32:30

+0

你好埃里克, 我已经提供了我的应用程序代码。你现在能想出来吗?谢谢 – KSL 2013-05-13 07:52:37

回答

0

对于这个答案我假设TodosTodoList的一个实例。我还以为你的错误是由该功能在您的AppView

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

解雇在那里你试图把你的Backbone.js的喜欢它是什么,一个集合例如列表Collection实例。但Backbone集合不是简单的列表,它们是具有属性models的对象,它是包含所有模型的列表。所以试图在对象上使用下划线的invoke(which works on lists)必然会导致错误。

但是别担心,骨干整齐地实现了许多下划线方法,其ModelCollection,including invoke。这意味着你可以调用集合中摧毁每个模型这样

SomeCollection.invoke('destroy'); 

希望这有助于!

+0

是的,运作良好,非常感谢! – KSL 2013-05-13 09:22:47