I'm curious about what people think of making it more convenient to load jQuery in MODX.
So many people are using jQuery, and specifying (as they should) a specific version, that there could easily be 3 or 4 versions loaded at the same time, and it's a pain having to update the jQuery source in multiple extras with no easy way to keep your own in sync., and no way at all to sync. with other extras.
Looking at donshakespeare's wonderful TinyMCE_wrapper got me to thinking about how to handle this (this is mostly off the top of my head). What I'm thinking:
1. New System Setting: jquery_source -- holds the path to a recent jQuery CDN version, e.g.
http://code.jquery.com/jquery-1.11.3.min.js, or a path to a local file. The benchmarks I've seen suggest that the jquery.com EdgeCast CDN has the fastest average load times. BTW, AFAIK, there is no longer a reliable way to load the latest version of jQuery -- both Google and jquery.com have stopped supporting this.
2. New $modx or $xpdo class variable: $jqueryLoaded
3. A convenience method in the $modx class: load_jquery()
public function load_jquery([string] $src = '', [bool] $forceLoad = false) {
$src = empty($source)? $modx->getOption('jquery_source') : $src;
if ( (!$this->jqueryLoaded) || $forceLoad) {
$this->regClientStartupScript('<script src="' . $src . '"></script>';
$this->jqueryLoaded = true;
}
}
Pros
- Convenience
- Reduced loading of multiple copies
- jQuery much more likely to be in the browser cache
- Synchronization across extras from different authors
- Faster page-load speeds for everyone
- Developers can still load a specific version of jQuery if they need to
Cons
- No easy way for developers to know which version of jQuery is loaded
- Potential for breaking extras when the System Setting is changed
- Slightly more baggage for MODX
In my experience, jQuery is pretty stable in the recent versions and the API works well across versions. IIRC, the API is the same for 1.7+ and jQuery 2+, the difference being that the 2.x versions no longer support older browsers. I doubt if changing the System Setting would cause much trouble.
This could also be done as an Extra that installed the System Setting and a snippet so that we could do this in our MODX Extras (though putting it in the $modx class is much cleaner, imo):
$modx->runsnippet('load_jquery');
The snippet could set and check a jqueryLoaded $_SESSION variable, or create a new $modx class variable.
I'm wondering, though if it belongs in the core. I'm usually against crapping up the core, but I think this might make sense.
Thoughts?