I am looking for the best way to control the global initialization of components / plugins / widgets in a large project. It has many components that work with jQuery that I would like to initialize quickly and efficiently, and after cleaning the Internet, I really found myopic examples that are realistic / realistic on small sites.
Problem
I want to find a smart and elegant way to get rid of this:
$(function() { $('.widget-one').widgetOne(); }); $(function() { $('.widget-two').widgetTwo(); }); $(function() { $('.widget-three').widgetThree(); }); $(function() { $('.widget-four').widgetFour(); });
Now, before you delete me for this, let me say that I know that in most cases (but not all). .widget-one is a bad selector, since it will receive all elements in dom and check the class in older browsers.
The problem is that these widgets are not one-time, and I may not know about the existence of time ahead of time (generated in the presentation of the web application, perhaps 2-3 times based on the logic or the product cycle or something else).
So, the following solutions are missing:
$(function() { $('#WidgetOne').widgetOne(); });
and
<span id="WidgetOne_12345">...</span> <script type="javascript"> $(function() { $('#WidgetOne_12345').widgetOne(); }); </script>
Thoughts
This is not a new problem. She has been around since day one. It still puzzles me how difficult it is to solve using jQuery even at this maturity level. Either that, or I'm missing something obviously obvious.
Unfortunately google-fu doesnβt do well on this issue, since everyone offers one of two things:
- jQuery
.live() or .delegate() catch-all event handlers. It is just scary on a fundamental level. .delegate() not so bad, but this requires the plugin / widget / control / everything to be a fully event-driven event. This will work in many cases for sure, but not in others. He also does the following and organizes the code very complex. I wonβt even go into .live() , for large complex sites, bubbling events is slow, and when you get enough components together, the list of queries that will correspond becomes large, making each click / focus / any event gradually slower overall for whole page. - Work with plugins such as liveQuery , which is a very cool plugin, but it seems that it was apparently designed to solve another problem (the problem of the appearance of new elements by creating ajax / dom) and will continue to increase gradually than more requests to be checked.
Conclusion
There must be a better way, I know there must be one. I have exhausted my google fu on this subject and still cannot find ideas / concepts / examples / discussion that are newer than jQuery 1.3.2, or with a big picture. In an ideal world, this will not be a problem, because everyone will use smart browsers with modern standards and a decent javascript engine, and .class requests will not occupy eons and eons, but, unfortunately, this is not so.
I am looking for ideas on how to handle this by going through many SO questions similar to this, and many articles about various jquery methods. I feel that if the information is there, then it is buried under many false positives and the β101 best jQuery pluginβ that appears in any search using jQuery. I know that someone there has encountered this predicament.
Ideas, links, examples, everything is welcome, there just has to be a better way.