0

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

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'
    });
})();
4

1 回答 1

1

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

initialize : function() {
        var self = this;
        this.model = new app.User();
        this.model.fetch({
            success : function(model, response, options) {
                self.render();
            }
        });
    }
于 2013-11-11T11:53:47.700 回答