2011-01-13 114 views
0

如果你有一個可變數目格式的jQuery插件:如何將函數名稱作爲參數應用jquery函數?

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


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

如果每個插件的定義是不能保證是相同的,可能包含下劃線後的名稱一定程度的變化,怎麼可能在一個情況下,這些應用如這樣的:

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 
}; 

我想用another question爲指導,但不知道怎樣正確地範圍了。任何指針將不勝感激。

謝謝:)

編輯: 對於那些想知道什麼功能在裏面,這是一個例子(從另一個學到了回答,我不能立即找到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); 

回答

0

jQueryCollection[pluginName](objSettings)不能正常工作嗎?

+0

嗨Yade。我現在嘗試2個選項。你先用一個例子:$('#tools')[pluginName](objSettings); this second:$ .fn [ pluginName]的.app ($('#tools'),objSettings);我馬上回來報告。 – MyStream 2011-01-13 04:42:15

相關問題