Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/375.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
Javascript &引用;无法调用方法';在';“未定义”的定义;问题_Javascript_Backbone.js - Fatal编程技术网

Javascript &引用;无法调用方法';在';“未定义”的定义;问题

Javascript &引用;无法调用方法';在';“未定义”的定义;问题,javascript,backbone.js,Javascript,Backbone.js,如何解决 查看: 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

如何解决

查看:

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
});
路由器:

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
});
收藏:

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