JSONP + Google Ajax Apis

66 views
Skip to first unread message

noiv

unread,
Oct 31, 2009, 8:37:20 PM10/31/09
to MooTools Users
Hi,

I want to use the google ajax api to get news in different languages
and display them with a tab control. The usual result does not know
the language, but I can use a context parameter. Unfortunately the
result then looks like this:

callbackFunction(
contextValue, // the context arg value
responseObject, // the collection of results and cursor
responseStatus, // 200 on success, non-200 on failure
errorDetails) // error string for non-200 response

note 4 parameters here. http://code.google.com/apis/ajaxsearch/documentation/reference.html#_fonje_args

The request uses onComplete : this.displayResult.bind(this) which
works fine without the context param, because the API answers with
only one param then.

I had a look into the code of mootools-1.2.4.1-more and replaced
'data' with 'arguments' here:

Request.JSONP.request_map['request_' + index] = function(data)
{ this.success(arguments, script); }.bind(this);

Now my function is called with an array of all the api arguments.

Is it safe to continue with this little diff or did I miss any side-
effects?

Aaron Newton

unread,
Oct 31, 2009, 11:22:46 PM10/31/09
to mootool...@googlegroups.com
That looks reasonable; I think it would make a good change in the code; I'll update the class for the next release.

To make it work properly, you'll need to update the success method:

success: function(args, script){
if (script) script.destroy();
this.running = false;
this.log('JSONP successfully retrieved: ', args);
this.fireEvent('complete', args).fireEvent('success', args).callChain();
}

they key being that the arguments are applied as they are, and not wrapped in an array (so fireEvent('complete', args) not fireEvent('complete', [args]).

Now your complete event will be passed all the arguments:

myJSONP.addEvent('complete', function(contextValue, responseObject, responseStatus, errorDetails){...});

noiv

unread,
Nov 1, 2009, 1:42:08 PM11/1/09
to MooTools Users
> this.fireEvent('complete', args).fireEvent('success', args).callChain();

Works like a charm, arguments hides some magic...
Reply all
Reply to author
Forward
0 new messages