【问题标题】:Backbone Models: Model returning empty骨干模型:模型返回空
【发布时间】:2013-02-04 20:07:50
【问题描述】:

我觉得我在这里遗漏了一些非常明显的东西。这是我的模型:

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()。我觉得它应该记录一个新模型。我尝试返回一个新模型,但我收到一个错误,即模型不是构造函数。我在这里错过了什么?

【问题讨论】:

    标签: backbone.js requirejs amd


    【解决方案1】:

    this.collection = new spamTermsCollection(); console.log(this.collection.toJSON()); // returns empty collection

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

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

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

    如果 fetch 始终是异步的,这应该可以工作,但如果由于某种原因它不在将来,您可能会遇到问题。您可以考虑将 fetch 移出 initialize 以确保:

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

    【讨论】:

    • 我知道这很明显。谢谢,保罗。
    猜你喜欢
    • 2013-10-14
    • 2014-06-25
    • 2012-05-04
    • 2012-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多