Answer the question
In order to leave comments, you need to log in
How to get click initiator in Backbone.js?
I study backbone. I'm writing dumbass. Along the way, there was a need to get the initiator of the click.
For example: If I clicked on .delete-task in the 3rd extra, then transfer it specifically to a variable. How can I best implement this?
var TaskView = Backbone.View.extend({
el: '#tasks',
events: {
'click > li .delete-task' : 'removeTask',
'click > li .complete-task' : 'completeTask'
},
removeTask : function () {
},
completeTask : function () {
}
});
Answer the question
In order to leave comments, you need to log in
Figured out how to make this work. Maybe someone is interested or looking for:
var TaskView = Backbone.View.extend({
el: '#tasks',
model: Todo,
events: {
'click > li .delete-task' : 'removeTask',
'click > li .complete-task' : 'completeTask'
},
removeTask : function (e) {
console.log($(e.target));
},
completeTask : function () {
}
});
More precisely e.currentTarget. In the event of an event bubbling, e.target may not be exactly what you need
javascript.ru/blog/Andrej-Paranichev/Vvedenie-sobytiya
>> If I clicked on .delete-task in the 3rd surplus, then transfer it specifically to a variable.
Hm. Firstly, a view must be created for each excess, with each view associated with the corresponding model with the corresponding id. Secondly, in this view, all events work only within $el, events outside your view will not be processed.
As a result, there should be two views - a container for all tasks and a view for one, and the removal is implemented in it in a similar way:
var TaskView = Backbone.View.extend({
model: Todo,
events: {
'click .delete-task' : 'removeTask'
},
removeTask : function () {
this.model.destroy();
}
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question