0

我的收藏是空的,但包含我的数组。我想在集合上执行 where ,但这是不可能的,因为 d 模型返回一个数组 [0]。所以数据不会被推送到那里。

我想这是因为我的数据是异步生成的

d {models: Array[0], length: 0, _byId: Object, constructor: function, model: function…} _byId: Object _events: Object _idAttr: "id" _listenerId: "l4" length: 15 models: Array[15 ] 原型:e

来自 app.js

var AppRouter = Backbone.Router.extend({
    routes: {
        "Onderwerpen": "subjectsList"

    },

    initialize: function  () {

        this.categories = new Categories();
        this.categories.getCategoriesFromJSON();

        console.log(this.categories);


        //view for subjectsList
        this.subjectsListView = new SubjectsListView({collection: this.categories});


    },



    subjectsList: function () {
        $('#app').html(this.subjectsListView.render().el);
    },   

});

var app = new AppRouter();

$(function() {
    Backbone.history.start();
});

来自 category.js

var Categories = Backbone.Collection.extend({
model : Category,
getCategoriesFromJSON : function(){


    var self = this,
        categories = [];

    $.ajax({
        url      : './catalogue.json',
        dataType : 'json'
    }).done(function(data){

        console.log('Catalogue retrieved');

        _.each(data.categories, function( categorieObj ){

            var categorieName       = _.keys(categorieObj)[0],
                categorieAttributes = categorieObj[categorieName];

            categories.push( categorieAttributes );

        });

        //console.log(JSON.stringify(categories));

        self.add( categories );
        self.trigger('reset');
    });
}


});

怎么了?

4

1 回答 1

0

找到了合适的解决方案。在启动主干应用程序之前,我在新函数 loadDataAndStartApp() 中检索 JSON 数据。当 dom 准备好时加载此函数,一旦加载 JSON,它就会启动主干应用程序。这样您就可以确定所有可用数据。

var AppRouter = Backbone.Router.extend({
routes: {
    "": "startscreen",
    "Onderwerpen": "subjectsList", //show list of subjects: eg Evenwicht, Zien & horen, etc
},

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     = [],
    category_trees = [];

// 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,
        category_trees : category_trees
    });

    Backbone.history.start();
});
}


$(function(){
// ON DOM READY

loadDataAndStartApp();
});
于 2013-11-09T12:27:11.803 回答