|
83 | 83 | 'callbackModalOpen' : null,
|
84 | 84 | 'callbackModalClose' : null,
|
85 | 85 | 'jsonpCallback' : null,
|
| 86 | + 'alert' : function (msg) { alert(msg); }, |
86 | 87 | // Language options
|
87 | 88 | 'geocodeErrorAlert' : 'Geocode was not successful for the following reason: ',
|
88 | 89 | 'addressErrorAlert' : 'Unable to find address',
|
|
1735 | 1736 | });
|
1736 | 1737 |
|
1737 | 1738 | // A really lightweight plugin wrapper around the constructor,
|
1738 |
| - // preventing against multiple instantiations |
| 1739 | + // preventing against multiple instantiations and allowing any |
| 1740 | + // public function (ie. a function whose name doesn't start |
| 1741 | + // with an underscore) to be called via the jQuery plugin, |
| 1742 | + // e.g. $(element).defaultPluginName('functionName', arg1, arg2) |
1739 | 1743 | $.fn[ pluginName ] = function (options) {
|
1740 |
| - this.each(function () { |
1741 |
| - if (!$.data(this, "plugin_" + pluginName)) { |
1742 |
| - $.data(this, "plugin_" + pluginName, new Plugin(this, options)); |
1743 |
| - } |
1744 |
| - }); |
| 1744 | + var args = arguments; |
| 1745 | + // Is the first parameter an object (options), or was omitted, instantiate a new instance of the plugin |
| 1746 | + if (options === undefined || typeof options === 'object') { |
| 1747 | + return this.each(function () { |
| 1748 | + // Only allow the plugin to be instantiated once, so we check that the element has no plugin instantiation yet |
| 1749 | + if (!$.data(this, 'plugin_' + pluginName)) { |
| 1750 | + // If it has no instance, create a new one, pass options to our plugin constructor, and store the plugin instance in the elements jQuery data object. |
| 1751 | + $.data(this, 'plugin_' + pluginName, new Plugin( this, options )); |
| 1752 | + } |
| 1753 | + }); |
| 1754 | + // Treat this as a call to a public method |
| 1755 | + } else if (typeof options === 'string' && options[0] !== '_' && options !== 'init') { |
| 1756 | + // Cache the method call to make it possible to return a value |
| 1757 | + var returns; |
1745 | 1758 |
|
1746 |
| - // chain jQuery functions |
1747 |
| - return this; |
| 1759 | + this.each(function () { |
| 1760 | + var instance = $.data(this, 'plugin_' + pluginName); |
| 1761 | + |
| 1762 | + // Tests that there's already a plugin-instance and checks that the requested public method exists |
| 1763 | + if (instance instanceof Plugin && typeof instance[options] === 'function') { |
| 1764 | + |
| 1765 | + // Call the method of our plugin instance, and pass it the supplied arguments. |
| 1766 | + returns = instance[options].apply( instance, Array.prototype.slice.call( args, 1 ) ); |
| 1767 | + } |
| 1768 | + |
| 1769 | + // Allow instances to be destroyed via the 'destroy' method |
| 1770 | + if (options === 'destroy') { |
| 1771 | + $.data(this, 'plugin_' + pluginName, null); |
| 1772 | + } |
| 1773 | + }); |
| 1774 | + |
| 1775 | + // If the earlier cached method gives a value back return the value, otherwise return this to preserve chainability. |
| 1776 | + return returns !== undefined ? returns : this; |
| 1777 | + } |
1748 | 1778 | };
|
1749 | 1779 |
|
1750 | 1780 |
|
|
0 commit comments