[英]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.model
是app.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.