2013-05-13 81 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