A
A
Alexander Seredenko2015-01-06 14:19:40
JavaScript
Alexander Seredenko, 2015-01-06 14:19:40

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 () {

        }
    });

Thank you.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander Seredenko, 2015-01-06
@san4ooo

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 () {

        }
    });

$(e.target) is the current clicked element.

D
diavol, 2015-01-06
@diavol

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

M
movetz, 2015-01-07
@movetz

>> 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 question

Ask a Question

731 491 924 answers to any question