0

If you have a variable number of jQuery plugins of the format:

(function($){
    $.fn.toolSet_themeOne = function () {
        // params
    };
})(jQuery);


(function($){
    $.fn.toolSet_themeTwo = function () {
        // params
    };
})(jQuery);

Where each plugin's definition is not guaranteed to be the same and is likely to contain some degree of variation in name after the underscore, how could these be applied in a situation such as this:

var themeApply = function (jQueryCollection, objSettings, pluginName) {
    // this does not work and reports pluginName is not a function
    jQueryCollection.pluginName(objSettings);
    // $.fn[pluginName] is reported as a function
};

I thought to use another question as a guide, but don't see how to scope it correctly. Any pointers would be greatly appreciated.

Thank you :)

EDIT: For those wondering what function is inside, this is an example (learned from another SO answer for which I cannot find an immediate refrence:

(function($){

    $.fn.toolSet_themeOne = function () {

        var methods = [
            update: update
        ];

        var init = function (options) {
            // normal jquery initialisation here
        };

        var update = function (data) {
            // do something with data
            // if not initialising, but calling after initialisation
        };

        // what do we do on initialisation?
        if(arguments.length > 0) {
    if('string' === typeof(arguments[0]) && 'undefined' !== typeof(methods[arguments[0]])) {
        var args = Array.prototype.slice.call(arguments);  
        args = args.shift();
        methods[arguments[0]].apply(this,args);
    } else if('object' === typeof(arguments[0])) {
        init.apply(this, arguments);
    }
    } else {
    init.apply(this, arguments);
    }
    return this;
    };
})(jQuery);
Community
  • 1
  • 1
MyStream
  • 2,533
  • 1
  • 16
  • 33

1 Answers1

0

Does jQueryCollection[pluginName](objSettings) not work?

Vadim
  • 17,897
  • 4
  • 38
  • 62
  • Hi Yade. I'm trying 2 options at the moment. Yours first with an example: $('#tools')[pluginName](objSettings); and this second: $.fn[pluginName].apply($('#tools'), objSettings); I'll report back in a moment. – MyStream Jan 13 '11 at 04:42
  • [~few min later~] Tried logging in to give you a score - open id didn't work - Both solutions above seem to work - you were spot on. I don't understand how it works - but it works for me :) Thanks Yads! – MyStream Jan 13 '11 at 04:47
  • @ArcaneErudite No problem, the way you were calling it was trying to find a method called pluginName on the jQueryCollection object. It's the same way you can access an object property/method by either saying obj.property or obj["property"] – Vadim Jan 13 '11 at 05:00
  • @Yads - I might have spoken too soon though, the plugin does get called but the arguments are empty. I'm wondering if that's an argument problem or scope issue. Checking now :) – MyStream Jan 13 '11 at 05:05
  • $('#tools')[pluginName](objSettings); This method works, but the second method doesn't. :) – MyStream Jan 13 '11 at 05:09
  • @ArcaneErudite, which second method? – Vadim Jan 13 '11 at 05:13
  • @Yads: $.fn[pluginName].apply($('#tools'), objSettings); Assuming objSettings is an object, it fails and reports that apply expects an array, so I create an array of the form var arrSettings = [objSettings]; and that reported an empty array when calling console.log(arguments); in the plugin file itself. – MyStream Jan 13 '11 at 05:27
  • @ArcaneErudite yes apply expects an array of arguments rather than a comma separated list. Try $.fn[pluginName].call($('#tools'), objSettings) – Vadim Jan 13 '11 at 14:17