Thursday, 15 May 2014

javascript - jQuery - make global variable available to multiple plugins -


I have a set of jQuery plugins, I'm making a website for

All these plugins are common functionality that they call $$ .getJSON ().

The URL passed in these calls varies depending on the Dev, QA and production environment.

I url in a central location so that it can be easily changed.

Where should the URL be placed? I do not want to store the URL in each individual plug-in. Can it be defined as a global variable or is it better to argue the plugin?

I recommend that you attach an object using a jQuery object. Just make sure to include it before all your plugins:

  jQuery.YourCompany = {url: "http://thedomain.com"};  

Then anywhere you need it, just use

  jQuery.YourCompany.url // or $ .YourCompany.url  

If you use objects / classes with the function $ .fn , you can also use this global variable as a namespace:

  jQuery.YourCompany PluginOne = function (el) {....} // But not on the FN object: jQuery.fn.pluginOne = function () {return.Each (function () {var p = New jQuery.YourCompany.PluginOne (this);}); }  

No comments:

Post a Comment