Javascript 无收集模型

Javascript 无收集模型,javascript,model-view-controller,backbone.js,Javascript,Model View Controller,Backbone.js,考虑以下模型和视图。我想知道为什么下面的指示行不起作用 var app = app || {}; (function () { app.CurrentUserView = Backbone.View.extend({ el: $('.avatar-container'), template: ux.template('.avatar-container-hbs'), initialize: function () {

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

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

您需要在
fetch
@niranjanbarawake的成功处理程序中调用
render
方法,您的意思是:
this.model.fetch().done((函数(){this.render();}))initialize
方法。@PHPst如何实例化视图?
new app.CurrentUserView()一切似乎都很好。您确定已添加
var self=this
初始化
功能开始时?我认为JSFIDLE对我们双方都有帮助:)我想知道为什么在todo示例中,主干渲染的应用程序并没有放在fetch函数中,但它仍然可以工作<代码>初始化:function(){this.collection=new app.Library();this.collection.fetch();this.render();
当您执行
this.collection.fetch()或
this.model.fetch()时,是否可以提供指向此示例应用程序的链接
AJAX
GET
请求被发送到服务器@声明
Collection
Model
时提到的
url
。由于这是一个异步请求,您必须在它的成功处理程序中完成自己的工作。希望这能说明问题。