简体   繁体   English

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

[英]Uncaught TypeError: Object function (){return i.apply(this,arguments)} has no method 'on'

For some reasons, I keep on getting this error, (See attached screenshot). 由于某些原因,我不断收到此错误,(请参阅随附的屏幕截图)。 I've tried adding a _.bindAll(this); 我尝试添加_.bindAll(this); and even tried upgrading my code to have the latest version of backbonejs. 甚至尝试升级我的代码以拥有最新版本的骨干js。 Still no luck. 仍然没有运气。

Can someone help me on this? 有人可以帮我吗?

在此处输入图片说明

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

Code for "collections/employees.js" “ 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();
})();

Code for "model/employee.js" 代码为“ model / employee.js”

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

    app.Employee = Backbone.Model.extend({
        defaults: {
            empName: '',
            department: '',
            designation: '' 
        }
    });
})();

You're saying this in your view: 您是这样说的:

model: app.Employee

app.Employee looks like a model "class" rather than a model instance. app.Employee看起来像模型“类”,而不是模型实例。 Your view wants a model instance in its model property. 您的视图在其model属性中需要一个模型实例。 Normally you'd say something like this: 通常,您会这样说:

var employee = new app.Employee(...);
var view = new app.EmployeeView({ model: employee });

this.model.toJSON() won't work since this.model is the app.Employee constructor. 由于this.modelapp.Employee构造函数,因此this.model.toJSON()将不起作用。 Actually I don't see any meaning in your EmployeeView.render method. 实际上,您的EmployeeView.render方法中没有任何意义。 If it is aggregate view why you have model on it? 如果是聚合视图,为什么要在其上进行建模? Otherwise what is the second view class EmployeeItemView ? 否则,第二个视图类EmployeeItemView什么? If you're following ToDo MVC example you can see that there is no model in AppView , that is why I conclude you need not model in your EmployeeView . 如果您遵循ToDo MVC示例,则可以看到AppView没有模型,这就是为什么我得出结论,您不需要在EmployeeView建模。 And render method you provided seems to belong to EmployeeItemView . 您提供的render方法似乎属于EmployeeItemView

Secondly, you call app.Employee.set which is also a call on a constructor not on an object. 其次,调用app.Employee.set ,这也是对构造函数而不是对对象的调用。 I think you meant 我想你是说

saveEntry: function(){
    console.log('Inside SaveEntry!');
    app.employees.create(this.newAttributes());
    console.log('After SaveEntry!');
},

If you want to pass a model to app.EmployeeItemView you should use callback argument. 如果要将模型传递给app.EmployeeItemView ,则应使用回调参数。

addEmployee: function (employee) {
    var view = new app.EmployeeItemView({ model: employee });
    $('#empInfo').append(view.render().el);
}

暂无
暂无

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

相关问题 Backbone.js和Require.js-未捕获的TypeError:对象函数(){return parent.apply(this,arguments); }没有方法“开” - Backbone.js and Require.js - Uncaught TypeError: Object function (){ return parent.apply(this, arguments); } has no method 'on' Sencha:未捕获的TypeError:对象函数(){h.apply(this,arguments)}没有方法'setActiveItem' - Sencha : Uncaught TypeError: Object function (){h.apply(this,arguments)} has no method 'setActiveItem' Backbone Error:Uncaught TypeError:Object function(){parent.apply(this,arguments); 没有'on'方法 - Backbone Error: Uncaught TypeError: Object function (){ parent.apply(this, arguments); } has no method 'on' 未捕获的TypeError:对象函数(a){返回新j(a)}没有方法'has' - Uncaught TypeError: Object function (a){return new j(a)} has no method 'has' 未捕获的TypeError:对象(JS函数)没有方法“应用” - Uncaught TypeError: Object (JS Function) has no method 'apply' 未捕获的TypeError:对象.NumCon没有方法'apply' - Uncaught TypeError: Object .NumCon has no method 'apply' 未捕获的TypeError:对象函数()没有方法项 - Uncaught TypeError: Object function () has no method items 未捕获的TypeError:对象函数()没有方法“替换” - Uncaught TypeError: Object function () has no method 'replace' 未捕获的TypeError:对象函数没有方法 - Uncaught TypeError: Object function has no method 未捕获的TypeError:对象[object Object]没有方法'apply' - Uncaught TypeError: Object [object Object] has no method 'apply'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM