jquery plugin development pattern
That how I usually implement my plugins:
(function($){
$.fn.plugingName = function(inSettings){
//code that extands settings with default
return this.each(function(){
new PluginClass($(this), settings);
});
}
PluginClass = function(jElem, settings){
//here I usually put variable and function in such a way to compose better
var events = {
onMouseUp : function(){
//some actions
},
开发者_如何学运维onMouseMove : function(event){
//some actions
},
//etc.
},
draw = {
drawGrid : function(){//some actions},
//etc.
},
//etc.
/*****************************
* This is a place of question
****************************/
}
})(jQuery);
I just want to know if there is a pattern to separate the algorithm from declaration part that I described above. Some thing like to put all your algorithm part inside
function main(){
}();
If there is a better approach to distinguish the main part of your algorithm. I hope I described everything clear.
All others improvment that might be done with represented code are also appreciate.
You mean putting the function main()
outside of the big wrapping function($){ ...
? But this is not desired, the wrapping function is there for the very reason not to overcrap the global namespace. This is the very clean design and it is desired. So don't be worry to use this standard pattern.
精彩评论