未捕获的TypeError:对象函数(){return i.apply(this,arguments)}没有方法'on'

Dav*_*d R 1 javascript backbone.js

出于某些原因,我继续收到此错误,(请参见随附的屏幕截图).我尝试添加一个_.bindAll(this);甚至尝试升级我的代码以获得最新版本的backbonejs.仍然没有运气.

有人可以帮我吗?

在此输入图像描述

var app = app || {};

(function ($) {
'use strict';

app.EmployeeView = Backbone.View.extend({
    el: '#container',
    model: app.Employee,
    events: {
        'click #save' : 'saveEntry'
    },
    initialize: function(){
        console.log('Inside Initialization!');
        this.$empName = this.$('#txtEmpName');
        this.$department = this.$('#txtDepartment');
        this.$designation = this.$('#txtDesignation');
        this.listenTo(app.employees, 'add', this.addEmployee);
        app.employees.fetch();
        console.log('End of Initialization!');
        //this.render();
    },
    render: function () {
        console.log('Inside Render!!');
        console.log(this.model);
        this.$el.html(this.template(this.model.toJSON()));
        console.log('Inside End of Render!!');
        return this;
    },
    newAttributes: function(){
        return{
            empName: this.$empName.val(),
            department: this.$department.val(),
            designation: this.$designation.val()
        };
    },

    saveEntry: function(){
        console.log('Inside SaveEntry!');
        //console.log(this.newAttributes());
        console.log('this.model');
        console.log(app.Employee);
        //app.employees.create(this.newAttributes());
        app.Employee.set(this.newAttributes());
        app.employees.add(app.Employee);
        console.log('After SaveEntry!');
    },
    addEmployee: function (todo) {
        var view = new app.EmployeeItemView({ model: app.Employee });
        $('#empInfo').append(view.render().el);
    }
})
})(jQuery);
Run Code Online (Sandbox Code Playgroud)

"collections/employees.js"的代码

var app = app || {};
(function (){
    console.log('Inside collection');
    var Employees = Backbone.Collection.extend({
        model: app.Employee,
        localStorage: new Backbone.LocalStorage('employee-db')
    });
    app.employees = new Employees();
})();
Run Code Online (Sandbox Code Playgroud)

"model/employee.js"的代码

var app = app || {};
(function(){
    'use strict';

    app.Employee = Backbone.Model.extend({
        defaults: {
            empName: '',
            department: '',
            designation: '' 
        }
    });
})();
Run Code Online (Sandbox Code Playgroud)

mu *_*ort 5

你在这个观点中这样说:

model: app.Employee
Run Code Online (Sandbox Code Playgroud)

app.Employee看起来像模型"类"而不是模型实例.您的视图需要其model属性中的模型实例.通常你会说这样的话:

var employee = new app.Employee(...);
var view = new app.EmployeeView({ model: employee });
Run Code Online (Sandbox Code Playgroud)