Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle custom jQuery events in Meteor?

Tags:

events

meteor

In Metor 0.3.5, when all events were jQuery events, I was able to use use jQuery UI Draggable and then handle the drag & dragstop events using a Metor event map:

Template.game.events['dragstop .card'] = function (e) {
    //stuff
};

But I just read this in the Meteor mailing list:

In 0.3.6, event maps no longer depend on jQuery

And sure enough, the above technique no longer seems to work – my dragstop handler isn't called at all now.

I'd greatly appreciate any advice as to how to achieve the same effect in 0.3.6.

like image 886
Emmett Avatar asked May 18 '12 04:05

Emmett


2 Answers

Nowadays, you can simply use body events to accomplish this the "Meteor" way:

Template.body.events({
    'dragstop #somedivid': function(e) {
        // Do stuff
    }
});
like image 82
occasl Avatar answered Sep 26 '22 03:09

occasl


Custom jQuery events can be bound with plain old jQuery, bypassing event maps altogether:

$(function () {
    $('body').on('dragstop', '.card', function (e) {
        //stuff
    });
});

Remember to use jQuery's on function to bind the handlers, since template elements are not necessarily included in the DOM at all times.

like image 25
Emmett Avatar answered Sep 27 '22 03:09

Emmett