Backbone.js collection.on("add") not working
Backbone.js collection.on("add") not working
我试图构建一个简单的简历应用程序。我试图在向集合添加模型数组时启动add
事件。事件应该简单地登录发生了某些事情的控制台中。不幸的是,似乎什么都没有发生。也没有记录错误。谁能给我点小费?
下面是事件处理程序:
$(document).ready(function(){
//INITIALIZATION
var resumeSectionCollection = new ResumeSectionCollection();
var resumeSectionView = new ResumeSectionView();
//setting up collection and adding data to it
resumeSectionCollection.on("add", this.logme,this);
resumeSectionCollection.add([careerObjectiveModel,workExperienceModel1,workExperienceModel2,workExperienceModel3,educationModel,softwareSkillsModel]);
});
下面是集合类:
var ResumeSectionCollection = Backbone.Collection.extend({
model: ResumeSectionModel,
logme: function(model){
console.log("in logme");
},
});
在您的$(document).ready
回调函数:
$(document).ready(function(){
//...
resumeSectionCollection.on("add", this.logme,this);
//...
});
在大多数情况下, this
将是window
,所以你实际上在说:
resumeSectionCollection.on("add", window.logme, window);
但是在window
中没有logme
函数,您的logme
函数在ResumeSectionCollection
集合中。您可能想要更像这样的内容:
var ResumeSectionCollection = Backbone.Collection.extend({
model: ResumeSectionModel,
initialize: function() {
this.on('add', this.logme, this);
// or
// this.listenTo(this, 'add', this.logme)
},
logme: function(model) {
console.log("in logme");
}
});
然后:
$(document).ready(function() {
var resumeSectionCollection = new ResumeSectionCollection();
var resumeSectionView = new ResumeSectionView();
resumeSectionCollection.add([ careerObjectiveModel, workExperienceModel1, workExperienceModel2, workExperienceModel3, educationModel, softwareSkillsModel ]);
});
相关文章:
- 铬:“;未捕获的语法错误:意外的标记:"
- jQuery is not loaded
- AngularJS JSON not arriving php
- 可以设置“;文件名"发生错误时显示的内联脚本标记的
- JS表单提交"无法使用Chrome数据保护程序加载此页面.尝试重新加载页面.调试信息:POST CISmtuK
- 检测电话窃听,即:<a href="电话:xxx">在UIWebview上
- $rootScope not working
- reactRedux is not defined
- jQuery document.ready not working
- 使用“+="操作人员
- //而不是在src=“”上使用http://"属性
- "未捕获的语法错误:意外的标记}"
- 可以<脚本类型=“;text/javascript”>window.location=“/"</
- "实例范围”;TypeScript类的getter/setter
- Javascript复选框函数:;缺少:在属性id之后"
- Javascript/Jquery Blob not showing Chrome PDF
- ReferenceError: not defined
- "“;变量未引用正确的对象
- JavaScript/jQuery - "$ is not defined- $function()"
- How do i formulate "if this.children.has.not.class"