Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/392.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.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 未捕获类型错误:对象函数(){return i.apply(this,arguments)}没有方法';在';_Javascript_Backbone.js - Fatal编程技术网

Javascript 未捕获类型错误:对象函数(){return i.apply(this,arguments)}没有方法';在';

Javascript 未捕获类型错误:对象函数(){return i.apply(this,arguments)}没有方法';在';,javascript,backbone.js,Javascript,Backbone.js,由于某些原因,我不断地得到这个错误(见附件截图)。我尝试添加了一个..bindAll(这个)甚至尝试升级我的代码以获得最新版本的backbonejs。还是不走运 有人能帮我吗 var app = app || {}; (function ($) { 'use strict'; app.EmployeeView = Backbone.View.extend({ el: '#container', model: app.Employee, events: {

由于某些原因,我不断地得到这个错误(见附件截图)。我尝试添加了一个
..bindAll(这个)
甚至尝试升级我的代码以获得最新版本的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);

“collections/employees.js”的代码

“model/employee.js”的代码


在你看来,你是这样说的:

model: app.Employee
app.Employee
看起来像一个模型“类”,而不是一个模型实例。您的视图希望在其
model
属性中有一个模型实例。通常你会这样说:

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

在你看来,你是这样说的:

model: app.Employee
app.Employee
看起来像一个模型“类”,而不是一个模型实例。您的视图希望在其
model
属性中有一个模型实例。通常你会这样说:

var employee = new app.Employee(...);
var view = new app.EmployeeView({ model: employee });
this.model.toJSON()
不起作用,因为
this.model
app.Employee
构造函数。实际上我看不出你的
EmployeeView.render方法有什么意义。如果它是聚合视图,为什么在它上面有模型?否则,第二个视图类是什么?如果您遵循ToDo MVC示例,您可以看到在
AppView
中没有模型,这就是为什么我得出结论,您不需要在
EmployeeView
中建模。您提供的
render
方法似乎属于
EmployeeItemView

其次,调用
app.Employee.set
,这也是对构造函数而不是对象的调用。我想你是说

saveEntry: function(){
    console.log('Inside SaveEntry!');
    app.employees.create(this.newAttributes());
    console.log('After SaveEntry!');
},
如果要将模型传递给app.EmployeeItemView
,则应使用回调参数

addEmployee: function (employee) {
    var view = new app.EmployeeItemView({ model: employee });
    $('#empInfo').append(view.render().el);
}
this.model.toJSON()
不起作用,因为
this.model
app.Employee
构造函数。实际上我看不出你的
EmployeeView.render方法有什么意义。如果它是聚合视图,为什么在它上面有模型?否则,第二个视图类是什么?如果您遵循ToDo MVC示例,您可以看到在
AppView
中没有模型,这就是为什么我得出结论,您不需要在
EmployeeView
中建模。您提供的
render
方法似乎属于
EmployeeItemView

其次,调用
app.Employee.set
,这也是对构造函数而不是对象的调用。我想你是说

saveEntry: function(){
    console.log('Inside SaveEntry!');
    app.employees.create(this.newAttributes());
    console.log('After SaveEntry!');
},
如果要将模型传递给app.EmployeeItemView
,则应使用回调参数

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


请添加您的代码。但是,我几乎可以肯定地告诉您,您没有正确地创建对象,并且没有对对象以外的构造函数调用
。@zaquest我已经添加了代码。但是,如果我将语句“this.listenTo(app.employees,'add',this.addEmployee);”移到“app.employees.fetch()”之后当显示另一个错误时,错误消失,“uncaughttypeerror:Object function(){return i.apply(this,arguments)}没有方法‘toJSON’”。您还可以在初始化
app.employees
app.employees
的位置添加代码吗?谢谢!。我现在已经添加了代码。请检查,请添加您的代码。但是,我几乎可以肯定地告诉您,您没有正确地创建对象,并且没有对对象以外的构造函数调用
。@zaquest我已经添加了代码。但是,如果我将语句“this.listenTo(app.employees,'add',this.addEmployee);”移到“app.employees.fetch()”之后当显示另一个错误时,错误消失,“uncaughttypeerror:Object function(){return i.apply(this,arguments)}没有方法‘toJSON’”。您还可以在初始化
app.employees
app.employees
的位置添加代码吗?谢谢!。我现在已经添加了代码。请检查嗨,谢谢你的回答!。然而,我只是在我的Employee应用程序中克隆TodoMVC示例。恐怕我在那里找不到这种实例化。(如果我错了,请纠正我)通常在初始化视图时传入模型:
new EmployeeView({model:employeeInstance})
克隆过程中有错误,如果这样做,TodoMVC将严重受损。你能链接到你为此克隆的TodoMVC部分吗?嗨,谢谢你的回答!。然而,我只是在我的Employee应用程序中克隆TodoMVC示例。恐怕我在那里找不到这种实例化。(如果我错了,请纠正我)通常在初始化视图时传入模型:
new EmployeeView({model:employeeInstance})
克隆过程中有错误,如果这样做,TodoMVC将严重受损。你能链接到你要为此克隆的TodoMVC部分吗?明白了!。。非常感谢!!:-)知道了!。。非常感谢!!:-)