[Moo] JSONP + Google Ajax Apis

3 messages Options
Embed this post
Permalink
noiv

[Moo] JSONP + Google Ajax Apis

Reply Threaded More More options
Print post
Permalink

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?
anutron

[Moo] Re: JSONP + Google Ajax Apis

Reply Threaded More More options
Print post
Permalink
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){...});

On Sat, Oct 31, 2009 at 5:37 PM, noiv <[hidden email]> wrote:

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?

The MooTools Tutorial: www.mootorial.com Clientcide: www.clientcide.com
noiv

[Moo] Re: JSONP + Google Ajax Apis

Reply Threaded More More options
Print post
Permalink

> this.fireEvent('complete', args).fireEvent('success', args).callChain();

Works like a charm, arguments hides some magic...