简体   繁体   English

“不能在'未定义'问题上调用方法'

[英]“Cannot call method 'on' of undefined” issue

How to solve it? 怎么解决?

View: 视图:

App.Views.ModelsIndex = Backbone.View.extend({
    tagName: 'div',
    initialize: function() {
        this.template = _.template($('#container').html());

        // Cannot call method 'on' of undefined
        this.model.on('change', this.render, this);
    },
    render: function() {
        $(this.el).html(this.template({'model' : this.model}));
        return this;
    }
});

Router: 路由器:

App.Routers.Models = Backbone.Router.extend({
    routes: {
        '': 'index',
        'admin/:id' : 'show'
    },
    initialize: function() {
        this.collection = new App.Collections.Models();
        this.collection.fetch();
    },
    index: function() {
        view = new App.Views.ModelsIndex({'collection' : this.collection});
        $('#container').html(view.render().el);
    },
    show: function(id) {
        alert('entry id:' + id);
    }
});

Collection: 采集:

App.Collections.Models = Backbone.Collection.extend({
    url: "/app_dev.php/partner/api/users/1"
    , model: App.Models.Model
});

Your router passes a collection to your view constructor 您的路由器将集合传递给您的视图构造函数

new App.Views.ModelsIndex({'collection' : this.collection});

but you use a model in your view : 但是你在视图中使用了一个模型:

this.model.on('change', this.render, this);

Pick one and stick with it : 选择一个并坚持下去:

App.Views.ModelsIndex = Backbone.View.extend({
    initialize: function() {
        this.template = _.template($('#container').html());
        this.collection.on('change', this.render, this);
    }
    ...
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM