Search code examples
javascriptbackbone.jsbackbone-events

Drop event in backbone view


I'm trying to add a drop event in my backbone view but I can't get it right. Below is the code I'm using and you can also check out this jsfiddle.

var View = Backbone.View.extend({

    events: {
        'drop .dropzone': 'drop'
    },

    drop: function(e) {
        e.preventDefault();
        alert('Dropping');
    },

    initialize: function() {},

    render: function() {}
});

$(function() {
    var view = new View({
        el: $('.dropzone');
    });
});​

Solution

  • if your view's el == .dropzone then you should do

    events: {
        'drop': 'drop'
    },
    

    if you specify just event type without selector it will listen to event on the view's root element - $('.dropzone') in this case.

    The way you did it here would try to find .dropzone element among children of .dropzone element - and clearly it would fail to find this element at all