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 无法读取属性';toJSON';未定义的+;骨干_Backbone.js - Fatal编程技术网

Backbone.js 无法读取属性';toJSON';未定义的+;骨干

Backbone.js 无法读取属性';toJSON';未定义的+;骨干,backbone.js,Backbone.js,我真的是一个新的骨干,在一个世界的混乱背后。现在,我正在尝试使用collection.get()方法呈现单个人的视图,最好不要遍历整个集合 以下是用户集合: App.Collections.UserCollection = Backbone.Collection.extend({ url: "/users", model: App.Models.User, initialize: function(){ console.log('users collec

我真的是一个新的骨干,在一个世界的混乱背后。现在,我正在尝试使用collection.get()方法呈现单个人的视图,最好不要遍历整个集合

以下是用户集合:

App.Collections.UserCollection = Backbone.Collection.extend({
    url: "/users",
    model: App.Models.User,

    initialize: function(){
        console.log('users collection');
    },

});
用户模型:

 App.Models.User = Backbone.Model.extend({
        rootURL: '/users',
        initialize: function(){
            console.log('User model being generated');
        }

    });
用户视图(所有用户):

和viewUser视图(单用户):

我以为我是在用户视图中获取单用户模型,并用这些行将其传递给ViewUser视图

var current_user_model = this.collection.get(currentUser);
var user = new App.Views.ViewUser({ model: current_user_model });
但我似乎不是。感谢您的帮助和/或解释,谢谢

我会这样做:

App.Views.Users = Backbone.View.extend({
        el: "body",
        model: 'user',

        initialize: function(){
            console.log('User view rendering');
            this.listenTo(this.collection, 'reset', this.addAll);
        },
        clearDiv: function(){
            var self = this;
            this.collection.fetch({
               success: function() {
                   var container = $('#render-area');
                   $('body').removeClass('homepage');
                   $('#main-nav').fadeIn(100);
                   container.empty();
                   self.addUser();
               }
            });
        },
        addUser: function(){
            var current_user_model = this.collection.get(currentUser);
            var user = new App.Views.ViewUser({ model: current_user_model });
            $('#render-area').append(user.el);
        },

        events: {
            'click #view-profile' : 'clearDiv'
        }

});

您是否检查了
this.collection.get
是否返回对象? 可能它无法获取
当前用户

在渲染视图之前,您应该

a) 在调用视图之前检查模型是否有效,并呈现“未找到”视图或 b) 在视图中,检查模型是否有效,并呈现“未找到”视图 c) 也可以在车把模板中处理

示例A(未经测试):

例B:

App.Views.ViewUser = Backbone.View.extend({

        initialize: function(){
            console.log("single user view");
            this.template = HandlebarsTemplates['user_profile'];
            this.templateNotFound = HandlebarsTemplates['user_profile_not_found'];
            this.render();
        },  
        render: function(){
            if(this.model) {
                $('#render-area').html(this.template(this.model.toJSON()));
            } else {
                $('#render-area').html(this.templateNotFound());
            }
        },
});
var current_user_model = this.collection.get(currentUser);
var user = new App.Views.ViewUser({ model: current_user_model });
例C:

App.Views.ViewUser = Backbone.View.extend({

        initialize: function(){
            console.log("single user view");
            this.template = HandlebarsTemplates['user_profile'];
            this.templateNotFound = HandlebarsTemplates['user_profile_not_found'];
            this.render();
        },  
        render: function(){
            if(!this.model) {
                this.model = new this.model(); // create model with defaults
            }
            $('#render-area').html(this.template(this.model.toJSON()));
        },
});
var current_user_model = this.collection.get(currentUser);
var user = new App.Views.ViewUser({ model: current_user_model });
所有示例都未经测试

希望这有点帮助,
best Carsten

在将模型传递给您的视图之前,您是否检查过模型中是否有任何内容?我敢打赌没有,但我不确定如何补救。感谢Francois,我将尝试一下谢谢您Carsten,我实际上已经放弃了这个特定项目的主干,因为我对它太陌生,而且在最后期限内,但我非常感谢您为我展示了一种测试正在发生的事情的方法,这是一种我将在未来尝试的技术:)
var current_user_model = this.collection.get(currentUser);
if (current_user_model) {
    var user = new App.Views.ViewUser({ model: current_user_model });
} else {
    new AppViews.ViewUserNotFound();
}
App.Views.ViewUser = Backbone.View.extend({

        initialize: function(){
            console.log("single user view");
            this.template = HandlebarsTemplates['user_profile'];
            this.templateNotFound = HandlebarsTemplates['user_profile_not_found'];
            this.render();
        },  
        render: function(){
            if(this.model) {
                $('#render-area').html(this.template(this.model.toJSON()));
            } else {
                $('#render-area').html(this.templateNotFound());
            }
        },
});
var current_user_model = this.collection.get(currentUser);
var user = new App.Views.ViewUser({ model: current_user_model });
App.Views.ViewUser = Backbone.View.extend({

        initialize: function(){
            console.log("single user view");
            this.template = HandlebarsTemplates['user_profile'];
            this.templateNotFound = HandlebarsTemplates['user_profile_not_found'];
            this.render();
        },  
        render: function(){
            if(!this.model) {
                this.model = new this.model(); // create model with defaults
            }
            $('#render-area').html(this.template(this.model.toJSON()));
        },
});
var current_user_model = this.collection.get(currentUser);
var user = new App.Views.ViewUser({ model: current_user_model });