I have build a little function which returns a jQuery plugin applied to a certain element, as in the following example:
var initiate_shop = function(shop_navigation, options, callback) {
var default_settings = {
containerID : "",
first : false,
previous : false,
next : false,
last : false,
startPage : 1,
perPage : 8,
midRange : 15,
startRange : 1,
endRange : 1,
keyBrowse : true,
scrollBrowse : false,
pause : 0,
clickStop : true,
delay : 50,
direction : "auto",
animation : "fadeInUp",
fallback : 400,
minHeight : true,
callback : function(pages, items){
if(typeof callback == 'function') {
callback.call(this);
}
}
};
var settings = $.extend(default_settings, options);
return $(shop_navigation).jPages(settings);
};
My question would be, how could I use that initiate_shop function like this:
$("shop_navigation").initiate_shop( ... );
Instead of using it like this:
initiate_shop(shop_navigation, ... );
Also, how can I define arguments for the callback function, in my situation I have two objects as arguments for the callback function ?
Looks like you’re trying to create a jQuery plugin. Extend the
$.fnobject with your function, and you will be able to access your container with thethiskeyword from within the function.Note that your call
$('shop_navigation')is not a valid selector, unless you’re looking for an element namedshop_navigation. Did you intend to write$('#shop_navigation')?The reason I’m asking is that you might equally well write
It is up to your plugin to deal with supporting multiple elements being passed in
this.The way you’re currently calling your callback, using
.call, is useful if you want to specify what the context, i.e. thethiskeyword, is in your callback function.Consider that your callback looks like this:
If you don’t care what the
thiskeyword is within your callback function, you could simply call the function asIf you want to manually set the calling context, you may use
.callor.apply. You can still pass arguments to the function:or
In your case, if
callbackvariable will only ever be a function, or undefined, and you’re not adding any additional logic, as it is right now, you’d get away with just: