tags:

views:

68

answers:

1

Is it possible to override jQuerys target in a custom event?

f.ex:

var scope = function() {
    this.param = 'foo';
}

var instance = new scope();

$(instance).bind('bar', function(e) {
    console.log(e.target);
});

$(instance).trigger('bar');

This outputs the entire instance object, as it should.

But if I want target to be something else, like param I can try:

var scope = function() {
    this.param = 'foo';
}

var instance = new scope();

$(instance).bind('bar', function(e) {
    console.log(e.target);
    console.log(e.customTarget);
});

$(instance).trigger({
    type: 'bar',
    target: instance.param, // override  attempt
    customTarget: instance.param
});

This outputs customTarget as 'foo', but target remains the same.

I need to override target, but it seems impossible? I also tried doing so via the jQuery.Event with no luck.

Anyone?

+1  A: 

Can't you just pass customTarget and re-assign target in the called eventhandler?

        var scope = function() {
        this.param = 'foo';
    }

    var instance = new scope();

    $(instance).bind('bar', function(e) {
        e.target = e.customTarget;
        // do stuff
    });

    $(instance).trigger({
        type: 'bar',
        customTarget: instance.param
    });
Kevin
well, I suppose I could, but that's not really the question here...
David