主干视图集合错误
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: function() {
this.collection = new app.User();
this.render();
},
如果通过this.events
对象正确设置了点击事件。它应该如你所期望的那样工作。你输入的代码一点问题都没有。由于如果通过主干events
设置事件,this
总是引用View实例
因此,只有两个可能的原因会导致错误。
-
this
没有引用View实例。这意味着您显式地更改了this
引用的内容,或者使用了类似$(this.el).on("click")
的内容,因为这将this
引用的内容更改为单击的元素。 -
调用
initialize
后,this.collection以某种方式被覆盖。你有可能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
},
相关文章:
- 无法修改Controls集合,因为控件包含代码块(即<%.%>).无法更正此错误
- 主干视图集合错误
- Meteor Update集合-未捕获错误:不允许.不受信任的代码只能通过ID更新文档.[403]
- Backbonej集合返回错误的数据
- 主干集合在调用 fetch() 时给出“未定义的 Ajax”错误
- 尝试从MongoDB BsonArray转换为IEnumerable
POCO实例的集合时引发错误 - 在使用 meteor 集合时从开发控制台收到“找不到方法 [404]”错误
- backbone.js-集合视图给出"TypeError:无法读取属性'el'未定义的“;错误
- 将Parameters添加到Backbone.js集合导致ArgumentError(筛选器方法的参数数目错误.)
- 过滤集合上的Angular watch侦听器在错误的时间调用
- Model.extend无法更新集合.Backbone.js错误
- 可以通过主干集合查看错误字符串不是函数
- 创建Backbone.js集合时出现未定义错误
- 主干:从JSON错误中创建集合
- 构建BackboneJS事件集合时的错误- new Event()抛出错误
- Onchange集合获取错误而不是函数
- 错误:集合还没有初始化
- 在AngularJS中使用ng-disabled来验证元素集合时,会出现$rootScope:infdig错误
- 保存带有空子文档集合的Mongoose文档会导致重复键错误
- 具有唯一字段的重复键错误集合