Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to send JQuery instance method as Google API callback?
    text
    copied!<p>Has anyone previously figured out how to send a (JQuery plugin) object-scoped method as a Google API callback? E.g. for the PageSpeed API.</p> <p>All the samples show you sending the name of a global function, but I want to somehow send the specific instance method, (or a way to re-look up the JQuery object).</p> <p>This is based on <a href="https://code.google.com/apis/pagespeedonline/v1/getting_started.html#invoking" rel="nofollow">their sample</a>:</p> <pre><code> var s = document.createElement('script'); s.type = 'text/javascript'; s.async = true; var query = [ 'url=' + url, 'callback=runCallbacks', 'key=' + settings.api_key, ].join('&amp;'); s.src = API_URL + query; document.head.insertBefore(s, null); </code></pre> <p>So I want to achieve is something like this call:</p> <pre><code> $('div.jq-google-plugin).MyGooglePlugin('callback',result); </code></pre> <p>I am setting up the call to Google from within my plugin's init method.</p> <p>e.g. I tried this - but this produces a 400 Bad Request from Google API.</p> <pre><code> var query = [ 'url=' + url, 'callback='+ encodeURI('function(result){$(\'div.jq-google-plugin\').MyGooglePlugin(\'callback\',result);}’, 'key=' + settings.api_key, ].join('&amp;'); </code></pre> <p>What I don’t really want to have to do is create some name-mangled global just to catch the responses for each plugin instance, but I think this is the only way? (This works BTW.)</p> <pre><code> var query = [ 'url=' + url, 'callback=myUniqueGlobalCallback', 'key=' + settings.api_key, ].join('&amp;'); myUniqueGlobalCallback = function(result){$('div.jq-google-plugin).MyGooglePlugin('callback',result);} </code></pre> <p>Thanks in advance for any insights! I guess the solution may not be Google API-specific, but could be around why it rejects any thing other than a global function name.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload