[英]Backbone view collection error
我是骨干的新手,我只是想让这个观点发挥作用。 我想从用户填写的表单中提供集合数据,然后将该用户重定向到其他位置。 但是,我一直收到一个未定义的错误,即:
Uncaught TypeError: undefined is not a function
以下是导致错误的代码:
//this lives within a Backbone view, as does the following constructor.
clickSubmitRegister: function(e) {
e.preventDefault();
var formData = {};
$('#registrationForm').find('input').each(function() {
value = $(this).val();
thisId = $(this).attr('id');
if (value != '') {
formData[thisId] = value;
}
});
console.log(formData); //this displays the correct data, all good
this.collection.create(formData); //this line throws me the error
},
initialize构造函数如下:
initialize: function() {
this.collection = new app.User();
this.render();
},
如果你通过this.events
对象正确设置click事件。它应该按照你的预期工作。你输入的代码完全没有问题。 由于this
总是指视图实例,如果你通过设置骨干事件events
因此,只有两个可能的原因导致错误。
this
并不是指查看instance.which意味着你改变了什么this
是指明确,或者你使用像$(this.el).on("click")
因为这改变了什么this
是指到点击的元素。
在调用initialize
之后,this.collection以某种方式被覆盖。 ofc你有可能将this.collection.create
替换this.collection.create
了。
因为你没有输入完整的代码,所以我们无法检查它们。
所以试试吧
//this lives within a Backbone view, as does the following constructor.
clickSubmitRegister: function(e) {
e.preventDefault();
var formData = {};
$('#registrationForm').find('input').each(function() {
value = $(this).val();
thisId = $(this).attr('id');
if (value != '') {
formData[thisId] = value;
}
});
console.log(formData); //this displays the correct data, all good
console.log(this); //to check which object 'this' refers
console.log(this.collection); //to check if this.collection got replaced or not
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.