[英]View events not firing - backbone
I am building a view at the moment, and when I click on a .organisation
link I want to fire my edit event, however on clicking this element, nothing is fired, and I cannot understand why. 目前,我正在构建视图,当我单击.organisation
链接时,我想触发我的编辑事件,但是,在单击此元素时,什么也没有触发,所以我不明白为什么。
Here is the code that builds my view, 这是构建我的观点的代码,
App.Views.groupsView = Backbone.View.extend({
el: '.app',
template: _.template( $('#tpl-groups-base').html() ),
events: {
},
initialize: function(options) {
this.render();
},
render: function() {
this.$el.html( this.template() );
var orgTab = new App.Views.OrganisationsTab({
collection : new App.Collections.Organisations
});
},
}); });
App.Views.OrganisationsTab = Backbone.View.extend({
el : '#organisations',
initialize: function() {
App.Collections.OrganisationCollection = this.collection;
this.collection.fetch();
this.collection.on('sync', this.render, this);
},
render: function() {
this.addAll();
return this;
},
addAll: function() {
App.Collections.OrganisationCollection.each(this.addOne, this);
},
addOne: function(organisation) {
var view = new App.Views.OrganisationView({
model : organisation
});
this.$el.append( view.render().el );
}
}); });
App.Views.OrganisationView = Backbone.View.extend({
tagName: 'a',
className:'group group--panel col-sm-3 organisation',
template : _.template( $('#tpl-single-group').html() ),
events: {
"click body" : "edit",
},
initialize: function() {
this.listenTo(this.model, 'change', this.render);
this.listenTo(this.model, 'destory', this.remove);
},
render: function() {
this.$el.html( this.template({
group: this.model.toJSON()
}));
return this;
},
edit: function(e) {
e.preventDefault();
console.log(this.model);
}
});
Why would I not be able to click on the organisation a that is created in the final view, and trigger my edit function? 为什么我不能单击在最终视图中创建的组织并触发我的编辑功能?
If you want to attach a click event listener to a child element of a view's el 如果要将click事件侦听器附加到视图el的子元素上
events: {
"click .organisation" : "edit",
},
If you want to attach it to the el 如果您想将其附加到El
events: {
"click" : "edit",
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.