Skip to content

Commit de02749

Browse files
committed
Added functionality for public/private methods
1 parent 6e42761 commit de02749

File tree

3 files changed

+77
-17
lines changed

3 files changed

+77
-17
lines changed

dist/js/jquery.storelocator.js

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
'callbackModalOpen' : null,
8484
'callbackModalClose' : null,
8585
'jsonpCallback' : null,
86+
'alert' : function (msg) { alert(msg); },
8687
// Language options
8788
'geocodeErrorAlert' : 'Geocode was not successful for the following reason: ',
8889
'addressErrorAlert' : 'Unable to find address',
@@ -1735,16 +1736,45 @@
17351736
});
17361737

17371738
// 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)
17391743
$.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;
17451758

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+
}
17481778
};
17491779

17501780

0 commit comments

Comments
 (0)