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
Backbone.js 主干模型:返回空的模型_Backbone.js_Requirejs_Amd - Fatal编程技术网

Backbone.js 主干模型:返回空的模型

Backbone.js 主干模型:返回空的模型,backbone.js,requirejs,amd,Backbone.js,Requirejs,Amd,我觉得我错过了一些非常明显的东西。这是我的模型: define([ 'underscore', 'backbone' ], function(_, Backbone) { var spamTermsModel = Backbone.Model.extend({}); return spamTermsModel; }); 以下是使用该模型的集合: define([ 'jquery', 'underscore', '

我觉得我错过了一些非常明显的东西。这是我的模型:

define([
    'underscore',
    'backbone'
], function(_, Backbone) {
    var spamTermsModel = Backbone.Model.extend({});

    return spamTermsModel;
});
以下是使用该模型的集合:

define([
        'jquery',
        'underscore',
        'backbone',
        'models/spamTermsModel'
    ],
    function($, _, Backbone, spamTermsModel) {
        var spamTermsCollection = Backbone.Collection.extend({
            model: spamTermsModel,
            url: 'spam-terms',
            initialize: function(models, options) {
                //console.log(models);
                this.fetch({
                    success: function(data, options) {
                        //console.log(data);  // logs data correctly
                    }
                });
            }
        });

        return spamTermsCollection;
    }
 );
以下是我称之为集合的视图:

define([
    'jquery',
    'underscore',
    'backbone',
    'models/spamTermsModel',
    'collections/spamTermsCollection'
], function($, _, Backbone, spamTermsModel, spamTermsCollection) {
    var searchTermsView = Backbone.View.extend({
        initialize: function() {
            var stm = new spamTermsModel();
            console.log(stm);  // returns function()
            this.collection = new spamTermsCollection();
            console.log(this.collection.toJSON());  // returns empty collection
        },
        retrieveTemplate: function(model) {
            return _.template($('#spamTermsTemplate').html(), model);
        },
        createList: function() {
            var list = '';
            _.each(this.collection.toJSON(), function (obj) {
                list += obj.term + ', ';
                //console.log(obj);
            });
            //console.log(this.collection.toJSON());
            return list;
        },
        render: function() {
            list = this.createList();
            this.$el.html(this.retrieveTemplate(list));
            return this;
        }
    });

    return searchTermsView;


   });
});
最后是我的路线:

router.on('route:updateSpamTerms', function() {
    require(['views/spamTermsView'], function(st) {
          s = new st();
          $('#web-leads').html(s.render().el);
    });

});

我认为问题出在我的模型中,因为它
console.log()
s function()。我觉得它应该记录一个新的模型。我尝试返回一个新模型,但我得到一个错误,该模型不是构造函数。我在这里遗漏了什么?

this.collection=new spamTermsCollection();
console.log(this.collection.toJSON());//返回空集合

这是因为您的获取是异步的,并且您正在尝试在成功获取之前访问它

您可以绑定到集合的
reset
事件,然后呈现集合视图

this.collection.on('reset',this.render,this))

如果提取始终是异步的,那么这应该是可行的,但是如果出于某种原因,将来不再是异步的,那么您可能会遇到问题。可以考虑从代码中移动取出>初始化 >以确保:

this.collection = new spamTermsCollection();
this.collection.on('reset', this.render, this);
this.collection.fetch();

我知道这是显而易见的。谢谢你,保罗。