无集合模型

Collection-less model

本文关键字:模型 集合      更新时间:2023-09-26

请考虑以下模型和视图。我想知道为什么遵循指示的行不起作用?

var app = app || {};
(function () {
    app.CurrentUserView = Backbone.View.extend({
        el: $('.avatar-container'),
        template: ux.template('.avatar-container-hbs'),
        initialize: function () {
            this.model = new app.User();
            this.model.fetch();
            x=this.model;
            //these two lines output expected values.
            console.log(this.model);
            console.log(this.model.toJSON());
            this.render();
        },

        // Re-render the titles of the post item.
        render: function () {
        //this does not work! (empty)
             this.$el.html(this.template(this.model.toJSON()));
            return this;
        }
    });
})();

型:

var app = app || {};
(function () {
    'use strict';
    app.User = Backbone.Model.extend({
        url:'api/user.php'
    });
})();

更新视图的初始化方法,如下所示:

initialize : function() {
        var self = this;
        this.model = new app.User();
        this.model.fetch({
            success : function(model, response, options) {
                self.render();
            }
        });
    }