views:

117

answers:

1

I'm developing a jquery gallery, and I want to parametrize the highlighter function to support any kind of lightboxes (fancybox for example). In my defaults I did something like:

$.fn.mygallery.defaults = {
    functionHighlighter: null
}

so in the code, creating the item, I'll check this property, and if it's setted to a function, I want to call it on the element:

if ((typeof opt.functionHighlighter) === "function") {
    opt.functionHighlighter.call(link);
}

where link is the anchor element containing the miniature image, with href setted to the original image. In my idea, when the plugin is called, I set the function with:

$("#gallery").mygallery({ functionHighlighter: $.fancybox });

but the "call" on the functionHighlighter executes directly the fancybox, that returns an empty area. What I want to emulate is the execution of

link.fancybox();

that correctly builds the plugin on link. Any ideas?

A: 

you need to set these as options:

jQuery.fn.pluginName = function (options) {

var defaults = {

    fancyObject: '',

    fancyPlugin: '',

    fancy: ''
};

var settings = $.extend({}, defaults, options);

return this.each(function () {

    //plugin code

    if (options.fancy){
      $.options.fancyPlugin(options.fancyObject);
    }


});

};

$(selector).pluginName({fancy:true, fancyPlugin: 'fancybox', fancyObject: $(selector)});
XGreen
Yes, there's already the extension of defaults, I omitted it just for brevity. Into the core of my plugin I can correctly retrieve the functionHighlighter as $.fancybox, my problem is that I don't know how to apply it to the object using the functionHighlighter reference.
tanathos
Updated it. maybe like this?
XGreen
In my plugin code I can't made a call like $.fancybox() just because I don't know if the function used as highlighter is the fancybox or another plugin (like lightbox). I've to generalize the call...
tanathos
I guess it would be like this. but haven't tried it. sorry in case it didn't work
XGreen
with options.fancyPlugin(options.fancyObject) I obtain the same result that with opt.functionHighlighter.call(link): the plugin fancybox executes immediatly and with an empty area...
tanathos
maybe add a condition to it?
XGreen
I updated the answer to set for each element if you want to use a blabox
XGreen