簡體   English   中英

“不能在'未定義'問題上調用方法'

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

怎么解決?

視圖:

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

路由器:

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

采集:

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

您的路由器將集合傳遞給您的視圖構造函數

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

但是你在視圖中使用了一個模型:

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

選擇一個並堅持下去:

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