Post by Shorty on Dec 2, 2012 14:51:59 GMT -8
This was originally in a pm between me and Peter, but he suggested, I post here, so others can benefit as well.
Dec 2, 2012 13:20:39 GMT -8 Shorty said:
So you remember how I asked if you could make your styles code work on the shout box and you said didn't want to because of the whole ajax thing and would have check everything that is posted. Well I need to do something similar to the members list. I need to edit the registered date, but the way i have it doesn't always work because when you change the sorting and such it doesn't affect it. I take it's because it uses Ajax and its not actually loading the page again. Is there any way you can help me get it to work please? It would be much appreciated! Dec 2, 2012 14:30:47 GMT -8 Peter said:
Yeah, little bit of a pain that is. Originally when working out a solution, I wrote a method to hijack the request so that I could modify the response, and then forward on the XMLHttpRequest object with the modified responseText. This proved to be awkward when passing on the data to the "success" callbacks.What I finally decided on was to bind a global "success" event to the table. The thing to note with this, is that it's a global event, not local to the request being made.
In the end I wrote a helper method for Yootil to bind the event but also allowing an AJAX URL match.
yootil.ajax.bind("success", $("table.list"), function(){
what.show_in_members_list();
}, "/members/list");
Here is the code I wrote for Yootil...
/**
* Namespace: yootil.ajax
* Useful methods for AJAX
*/
yootil.ajax = (function(){
return {
/**
* Method: bind
* When we call .set() on a key, we can't specify a callback for when it's done. So this method allows
* us to do just that. This isn't ideal though, but works for now until we get a callback added in by
* ProBoards officially.
*
* Parameters:
* event - *string* The ajax event to bind (i.e "complete"), without "ajax" prefix.
* e - *object* The element to bind the event too.
* f - *function* This is the callback function that will get called.
* url - *string* / *boolean* The AJAX URL ProBoards calls to match against. If bool, match all.
* context - *object* The context ("this") of the callback function.
*
* Returns:
* *object* yootil
*
* Examples:
* yootil.ajax.bind("complete", $("form:first"), function(){ alert("AJAX completed"); });
*
* yootil.ajax.bind("complete", $("form:first"), function(){ alert("AJAX completed"); }, "/plugin/key/set/");
*/
bind: function(event, e, f, url, context){
var elem = $(e);
event = "ajax" + event.substr(0, 1).toUpperCase() + event.substr(1);
if(elem.length == 1){
context = (context)? context : e;
if(event && f && e.length){
elem[event](function(event, XMLHttpRequest, options){
if(url === true || new RegExp(url, "i").test(options.url)){
$.proxy(f, context, event, XMLHttpRequest, options, e)();
}
});
}
}
return yootil;
}
};
})();
Hope that gives you an idea.