Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
型号为isn&x27;t在调用集合时填充数据。主干和Django Tastypie_Django_Backbone.js_Backbone Views_Tastypie - Fatal编程技术网

型号为isn&x27;t在调用集合时填充数据。主干和Django Tastypie

型号为isn&x27;t在调用集合时填充数据。主干和Django Tastypie,django,backbone.js,backbone-views,tastypie,Django,Backbone.js,Backbone Views,Tastypie,当我的集合视图实例化时,我很难理解为什么模型没有显示。数组被调用,当我调用notes.models时,我得到了正确数量的记录。但是,如果要调用console.log(注意),则不会发生任何事情。因此,fetch似乎正在工作,并且有数据,我可以在响应中看到它,它只是没有被传递到集合中。欢迎任何帮助。这些都是 TastpieCollection和TastTypeModel来自于它,似乎工作得非常好 $(function() { // Note: The model and collecti

当我的集合视图实例化时,我很难理解为什么模型没有显示。数组被调用,当我调用notes.models时,我得到了正确数量的记录。但是,如果要调用
console.log(注意)
,则不会发生任何事情。因此,fetch似乎正在工作,并且有数据,我可以在响应中看到它,它只是没有被传递到
集合中。欢迎任何帮助。这些都是

TastpieCollection和TastTypeModel来自于它,似乎工作得非常好

$(function() {

    // Note: The model and collection are extended from TastypieModel and TastypieCollection
    // to handle the parsing and URLs

    window.Note = TastypieModel.extend({});

    window.Notes = TastypieCollection.extend({
        model: Note,
        url: NOTES_API_URL
    });

    // starts by assigning the collection to a variable so that it can load the collection
    window.notes = new Notes();

    window.NoteView = Backbone.View.extend({

        className: "panel panel-default note",
        template: _.template($('#notes-item').html()),

        initialize: function () {
            _.bindAll(this, 'render');
            this.model.bind('change', this.render);
        },
        render: function() {
            this.$el.html(this.template(this.model.toJSON()));
            return this;
        }
    });

    window.NoteListView = Backbone.View.extend({

        id: "notes-block",
        className: "panel panel-info",
        template: _.template($('#notes-item-list').html()),

        initialize: function () {
            _.bindAll(this, 'render');
            this.collection.bind('change', this.render);
        },
        render: function() {
            this.$el.html(this.template());
            this.collection.each(function(note){
                //var view = new NoteView({ model: note });
                console.log(note);
                //$('#notes-list').append(view.render().el);
            });
            console.log('done');
            return this;
        }
    });

    window.NotesRouter = Backbone.Router.extend({
        routes: {
            "": "list",
            'blank': 'blank'
        },
        initialize: function() {
            this.notesView = new NoteListView({
                collection: window.notes
            });
            notes.fetch();
        },
        list: function () {
            $('#app').empty();
            $('#app').append(this.notesView.render().el);
            console.log(notes.length);
        },
        blank: function() {
            $('#app').empty();
            $('#app').text('Another view');
        }
    });

    window.notesRouter = new NotesRouter();
    Backbone.history.start();
})

答案是它正在发挥作用。这不是我想要的工作方式,但上面的代码确实有效。因此,我将以不同的方式问这个问题,以触及问题的核心。

在尝试
此.collection.each
之前,您确定
获取
(这是一个AJAX调用)已从服务器返回吗?我非常确定。我把笔记移到了一堆不同的地方。没有变化。为了检查,我删除了fetch,所以它不调用任何东西,notes.models中也没有任何内容。因此提取工作正常。请尝试使用
console.log(this.collection.toJSON())
console.log
将实时引用放入控制台,因此您在控制台中看到的内容不一定是调用
console.log
时的内容。它甚至没有进入该循环。循环没有返回。您没有在
NoteListView
的任何位置调用
render
?呼叫就在路由器的
列表中。