Widget Factory: private methods vs outside-widget functions for large amounts of code
I'm developing an application-specific widget toolkit using the jQuery UI Widget Factory. The idea is to have a set of widgets which handle various aspects of working with simulation models: displaying model diagrams, plotting simulation results, etc.
Most widgets therefore require a fair bit of code (100s of lines) for handling their specific task. My problem is that I'm not sure where the relevant functions should go within the file holding the widget script.
Some guides on the Widget Factory show such functions being implemented as private methods in the widget's prototype. I.e. the method _refresh() below:
- $(function() {
- $.widget( "custom.widget1",{
- options: {...},
- _create: function() {
- ...
- this.refresh();
- },
- _setOptions: function(options) {...},
- _setOption: function(key, value) {...},
- _refresh: function() {...}
- });
- }(jQuery))
Private methods are methods that start with an underscore. They are expected to be accessed directly using the this keyword. Private methods are common and recommended.
The other pattern that I have seen is to include such function(s) outside the widget definition, e.g.
- $(function() {
- $.widget( "custom.widget1",{
- options: {...},
- _create: function() {
- ...
- refresh(this);
- },
- _setOptions: function(options) {...},
- _setOption: function(key, value) {...},
- });
- function refresh(widget) {...}
- }(jQuery))
For each of the options that changes the DOM, we call a specific helper method. The helper methods, createBars, createMarkers and createTickBar are specified outside of the widget instance properties. This is because they are the same for all widgets and need not be created individually for each widget instance.
This implies that there is a considerable overhead, for widgets with a lot of code that have multiple instances, in having the heavy-lifting functions implemented as private methods.
Are there accepted principles for using one pattern rather than the other when the amount of code involved is large?
Thanks,
Robert