[英]Backbone.js: Uncaught TypeError: Object #<Object> has no method 'get'
[英]Backbone.js Uncaught TypeError: Object [object Array] has no method 'on'
有人可以幫我嗎? 我不知道怎么了。
從json文件獲取數據:
this.categoryTrees = new CategoryTrees();
this.categoryTrees.getCategoryTreesFromJSON();
this.categories = new Categories();
this.categories.getCategoriesFromJSON();
為此創建視圖:
//view for startscreen
this.startscreenView = new StartscreenView({collection: this.categoryTrees});
//view for subjectsList
this.subjectsListView = new SubjectsListView({collection: this.categories.where({ category_tree : "onderwerpen" }) });
第二個帶有where子句的錯誤給了我:未捕獲的TypeError:對象[object Array]沒有方法'on'
當我不放置where子句時,它就可以正常工作。 在控制台中,我能夠執行where功能,而不會出現任何問題。
我在這里想念什么嗎?
where
方法將返回集合中與傳遞的屬性匹配的所有模型的數組 。 因此它不會返回Backbone.Collection
因此會出現on
undefined錯誤。
您需要根據where
的結果創建一個新的Categories
集合:
this.subjectsListView = new SubjectsListView({
collection: new Categories(
this.categories.where({ category_tree : "onderwerpen" }))
});
找到了正確的解決方案。 啟動主干應用程序之前,我要在新函數loadDataAndStartApp()中檢索JSON數據。 當dom准備就緒時,將加載此函數,並且一旦加載JSON,它將啟動主干應用程序。 這樣,您可以肯定所有數據都可用。 連同nemesv所說的關於創建新類別函數的內容,以及他的答案中的其他建議,我得出了針對該問題的以下答案。
var AppRouter = Backbone.Router.extend({
routes: {
"": "startscreen",
"Onderwerpen": "subjectsList"
},
initialize: function (options) {
this.categories = new Categories( options.categories );
this.subjectsListView2 = new SubjectsListView({
collection: new Categories(
this.categories.where({ parent : null, name : "Evenwicht" })
)
});
//view for spacesList
},
startscreen: function () {
$('#app').html(this.startscreenView.render().el);
},
subjectsList: function () {
$('#app').append(this.subjectsListView2.render().el);
},
});
function loadDataAndStartApp(){
var categories = []
// LOAD APP DATA
$.ajax({
url : './catalogue.json',
dataType : 'json'
}).done(function(data){
console.log('Catalogue retrieved');
// GET CATEGORIES
_.each(data.categories, function( categorieObj ){
var categorieName = _.keys(categorieObj)[0],
categorieAttributes = categorieObj[categorieName];
categories.push( categorieAttributes );
});
// START APP
var app = new AppRouter({
categories : categories
});
Backbone.history.start();
});
}
$(function(){
// ON DOM READY
loadDataAndStartApp();
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.