#137 closed feature (worksforme)
Disable $ Function
Reported by: | john | Owned by: | |
---|---|---|---|
Priority: | major | Milestone: | 1.1a |
Component: | core | Version: | 1.1a |
Keywords: | Cc: | ||
Blocked by: | Blocking: |
Description (last modified by )
This can be used as a way to use Prototype and jQuery at the same time. The code could look something like this:
jQuery.disableAlias();
Making any instance of '$' that it overwrote revert back to its old value.
Change History (10)
comment:1 Changed 17 years ago by
Milestone: | 1.0 |
---|---|
Owner: | changed from john to paul |
Version: | 1.0 |
comment:2 Changed 16 years ago by
comment:3 Changed 16 years ago by
Description: | modified (diff) |
---|---|
Milestone: | → 1.1 |
Summary: | Prototype Support Plugin → Disable $ Function |
Version: | → 1.1 |
comment:4 Changed 16 years ago by
Owner: | changed from paul to john |
---|
comment:5 Changed 16 years ago by
The hash idea is clever but all it takes is one line of code to make jQuery play nicely with libraries that use the $ alias.
$ = _$;
Now jQuery
can be used or jQuery
could be aliased to something else with just one line.
var $J = jQuery;
Here is a way to wrap this functionality up in a method (untested).
jQuery.changeAlias = function(alias) { $ = _$; window[alias] = jQuery; }:
and used like so:
jQuery.changeAlias('$J');
I'm still not sure that this should go in the core. If it should go into the core, then maybe it should look more like this (just coding off the top of my head, untested):
jQuery.changeAlias = function(alias) { if (jQuery.savedAlias) window[jQuery.savedAlias[0]] = jQuery.savedAlias[1]; jQuery.savedAlias = null; if (window[alias] != undefined) jQuery.savedAlias = [alias, window[alias]]; window[alias] = jQuery; };
Then in the core we should use it to set the '$' alias so that when this method is called it will restore the original alias if it is being used.
comment:6 Changed 16 years ago by
If the incompatibilities between jQuery and the rest of the world are limited to the $, please fix it asap. It's much better to have friends than enemies, and it would be nice to have bridges too, maybe by means of json?
comment:7 Changed 16 years ago by
One thing I like about the hash idea is that it takes care of the situation where another script/library has initialization code that uses $() and is automatically run onload. As long as jQuery is loaded before the other script, its alias will have already changed by the time the other script's init code is run.
That being said, the only really reliable way I can think of to get the hash is to put an ID (like jQuerySRC or something) on jquery.js's script element (Anonymous' code, as he states, only seems to work in Firefox), which adds another step to the process.
jQuery.changeAlias() is probably easier to deal with overall, though; you just need to be sure to call it before any other code that uses $() runs.
comment:8 Changed 16 years ago by
Having the ability to change the alias if needed should be fine for anyone looking to use the other libraries that use the $ shortcut. I'd vote for .changeAlias, but I'm not sure if it's necessary in the core. Developers would be using this, not many newbies, so perhaps it could be a plugin/option when people build their distro.
comment:9 Changed 16 years ago by
Resolution: | → worksforme |
---|---|
Status: | new → closed |
If you build your own distribution, you can simply change the alias in the source. I just moved the declartion more to the start of jquery.js, so it can be found easier.
If you expect jQuery to be simply compatible with everything else, a disableAlias function won't help either.
comment:10 Changed 11 years ago by
js's script element (Anonymous' code, as he states, only seems to work in Firefox), which adds another step to the process. michael kors handbags
Is the main issue here making jQuery compatible with Prototype, or just making it compatible with all other libraries (now including mootools et al) which use '$' as a shortcut in the global namespace?
I think it's worth pointing out that jquery.js is written well enough that it doesn't have to map the jQuery function to $, except as a convenience to users, and for backwards-compatibility with existing plugins. There is (presumably) no future-compatible way to consistently merge the output of jQuery and Prototype's '$' functions together -- and this would remain ugly and messy, in any case. My suggestion would be providing the option to map jQuery to a different shortcut (like $J, for example), and not override prototype's $, if this is what the user wants.
There are a number of ways this could be implemented:
var $ = jQuery
. Messy at best, and forces people to serve the uncompressed version.var overWritePrototype = false
. Again, messy and ugly.http://path.to/jquery.js#$J
.The script could check for such a hash, and if present, map jQuery to the specified shortcut instead of $. This is an example implementation (which works in firefox at least):
...which would fall back to the safe effect as
var $ = jQuery;
.