views:

23

answers:

2

I've attached some live() listeners in order to automatically make ajax calls for every link with ajax in the url:

$("document").ready(function() {
    $('a[href^="/ajax"]').live('click', call);
});

function call(e, context, link) {
    e && e.preventDefault();
    link = link || this;
    if(typeof link == "string" || !$(link).hasClass("disabled")) {
        newObj(SPZ.AjaxCall, link, context);
    }
};

But sometimes I want to override this (so that I can specify a context in which to find callback functions), so I wrote a jQuery method to do this

$.fn.customAjax = function(context) {
    return this.die().click(function(e){
        call.call(this, e, context);
    });
};

$(".save").customAjax(myObj);

The problem I have is that if the live handler is added to a collection of elements I can't just use die() on one of those elements; die() only works, it seems, if you apply it to all the elements live() was used on.

Can anyone suggest a workaround that will suppress the live() event? And, as an aside, what are people's opinions on suggesting the jQuery team change the behaviour of die() so that it can be used to remove live() handlers on individual elements; are there any reasons why this would be a bad idea?

A: 
elusive
Functions hanging off `$.fn` like in the OP's code automatically get `this` pointing to the current jQuery object instance. No need to wrap.
Roatin Marth
oops - will add the definition of call into the code above."this" is a jQuery object: the click handler does run with the context I specify. The trouble is that immediately afterwards the live handler runs as well
wheresrhys
A: 

A bit more playing around and I found that adding a simple return false at the end of my jQuery method did the trick. It'll cause problems if I ever want to attach other listeners for the same click, but it'll do for now.

wheresrhys