What's the pattern for a JSONP method that was initiated from a jQuery plugin?
- by michielvoo
I'm writing a jQuery plugin to render data retrieved from another domain in an element on the page. I follow the typical pattern for my jQuery plugin:
$(selector).Plugin(options);
In the plugin I get external data using jQuery.getScript(url, [success]). The external data source allows me to define the name of a method and it will wrap the data in a call to that method (JSONP):
$.getScript("http://www.example.com/data?callback=global_callback", instance_callback);
This effectively results in:
<script type="text/javascript">
global_callback(data);
</script>
The scope of global_callback limits what the Plugin instance can do with the data. And the global_callback method has no knowledge of the selector or options that the plugin was instantiated with.
I was thinking that global_callback would just store the data, and the plugin would retrieve the data in instance_callback. But I need to make sure that instance_callback will retrieve the correct data, I foresee a problem with multiple instances of the Plugin. How can I handle this?
Thanks!