Unique JQuery Events for a Class

Posted by Daniel Macias on Stack Overflow See other posts from Stack Overflow or by Daniel Macias
Published on 2011-11-29T01:24:57Z Indexed on 2011/11/29 1:50 UTC
Read the original article Hit count: 305

I am trying to create a class that can send a unique jQuery event. Example:

function Bomb(id) {
    this.evnt = $.Event("BOOM!_" + id);
    this.detonate = function() {
        $(document).trigger(evnt);
    };
}
var firecracker = new Bomb();
var nuclearbomb = new Bomb();

$(document).bind(firecracker.evnt.type, function(){
    // It's the fourth of july!!!
});

$(document).bind(nuclearbomb.evnt.type, function(){
    // We're dead
});

firecracker.detonate();
nuclearbomb.detonate();

How can I create a unique event within the Bomb class without having to pass in an ID to create a unique event string for the class?

© Stack Overflow or respective owner

Related posts about jquery-events

Related posts about uniqueidentifier