Jeremy brought this up as it might improve code readability, and also
save some bytes.
As an alternative to introducing such a global variable, we could use
closures for creating local aliases[1] within the respective modules.
Another alternative, of course, is to just keep it as "jQuery" (at least
for now, until we have more experience and/or a stronger use case).
Personally, I don't like the "jq" name, also because it seems like we're
creating an arbitrary name specific to the TiddlyWiki community (FWIW,
the jQuery documentation uses $j[2]).
I also think that such a global shortcut is not required.
Any thoughts?
-- F.
[1] http://tiddlywiki.org/wiki/Dev:Best_Practices#Creating_Aliases
[2] http://docs.jquery.com/Using_jQuery_with_Other_Libraries
> There's a ticket suggesting we introduce an alias/shortcut for the
> global jQuery variable:
> http://trac.tiddlywiki.org/ticket/1032
Um, do we want a thread here, or on the ticket?
> As an alternative to introducing such a global variable, we could use
> closures for creating local aliases[1] within the respective modules.
I like that approach for plugins, given $ is my preference for a
jQuery abbreviation.
> Another alternative, of course, is to just keep it as "jQuery" (at
> least
> for now, until we have more experience and/or a stronger use case).
> Personally, I don't like the "jq" name, also because it seems like
> we're
> creating an arbitrary name specific to the TiddlyWiki community (FWIW,
> the jQuery documentation uses $j[2]).
> I also think that such a global shortcut is not required.
Keeping to as many jQuery established conventions will help, so I'd
suggest avoiding jq for now, given that depends upon TiddlyWiki and
it's jQuery which provides "jquery".
Paul (psd)
--
http://blog.whatfettle.com
The ticket didn't seem to draw much attention, and this groups seems
more suitable for extensive discussions.
> Although it might seem innocuous (and even somewhat elegant), the
> 'local alias' technique for defining $(...) should probably be avoided
> within TiddlyWiki, because it can still create conflicts
That's a good point, Eric.
If I understand this correctly, though, it might suffice to ensure that
the $ alias is not used where eval is invoked?
However, that could become a bit messy. (I'd have to think on this a
little more.)
> If the jQuery docs are using $j as an alternative abbreviation, then
> I think that's what we should use as well.
I've just brought this up on jQuery's IRC channel. People there suggest
that $j is indeed the most commonly used shortcut name.
> Even so, it would be good to have an abbreviation for "jQuery" would
> save a considerable number of bytes in the TW core source code...
We might be engaging in premature optimization here.
So I'm with Paul in that I think we should defer this issue until we
actually have more jQuery code in the core.
However, I can see the arguments for nailing it down now.
-- F.
You know, I don't see "$" as being anything too special given there
are other clashes between libraries and TiddlyWiki's extension of
String, Date, etc.
I guess I'm saying I can live with loss of backwards compatibility
with external libraries introduced by plugins and as with other
similar clashes, and think the onus is on the plugin author to resolve
such clashes, not TiddlyWiki.
I don't believe this is correct - please take a look at this test case:
http://fnd.lewcid.org/tmp/alias_scope.html
It boils down to the following:
---------------
var $ = "lorem ipsum";
var foo;
(function($) {
foo = function() {
console.log(typeof $); // prints "function", referencing jQuery
};
// hijack
foo_orig = foo;
foo = function() {
foo_orig.apply(this, arguments);
console.log(typeof $); // prints "string", referencing custom $
};
foo(); // prints both "function" and "string"
---------------
Am I missing something?
-- F.
While I don't necessarily agree about the scoping concerns (there should
never be any "leaking", except when using eval), I do agree that this
introduces an uncertainty factor due to increased complexity.
I have thus reverted the relevant commits, so we're now back to simply
using jQuery:
http://trac.tiddlywiki.org/ticket/1032#comment:5
-- F.
I think this might be a better thread to help explain my position, and
how I think we should proceed on this issue.
> On Apr 21, 4:26 am, FND <F...@gmx.net> wrote:
>>> not just 'eval'... custom macro handlers, toolbar handlers, hijacks,
>>> etc... can also get invoked within the scope of a locally-declared $
>>> (...) alias
>>
>> I don't believe this is correct - please take a look at this test
>> case:
>> http://fnd.lewcid.org/tmp/alias_scope.html
>>
> As your tests illustrate, understanding and recognizing the current
> scope of $(...) can be difficult, especially when considering the use
> of hijacks, plugins, inline scripts and third-party libraries.
I do feel we may be being overly cautious, and if there is scope
leakage then it will help refactoring and plugin authors if we
understand the problem better and have regression tests to follow.
At the moment I can't help feel that our not fully understanding the
issues is leading to classic cargo-cult programming, and not in the
good sense.
The answer has to be to develop more test cases for such scenarios.
> Even with these tests, it's still not clear to me when the scope of $
> would be problematic and when it would be OK.
Again this cries out "more test cases"!
> In any case, keep in
> mind that TiddlyWiki is not just an open source application... it's an
> open source *development platform*... thus, while it might be possible
> for us to do the work to avoid a $(...) scoping problem within the
> core code, there is still a great potential for conflicts with other
> code that could create a major burden for anyone else attempting to
> develop new features.
I think I see this differently. As I understand it, the problem stems
when plugins introduce other frameworks which use "$".
We can work-around, and in the worst case, I still see it as the duty
of the developer of a plugin to work-round the clash, as it would be
if they introduced something which clashed with another existing core
library function.
> Except for the core team, we are already facing a paucity of
> TiddlyWiki developers in the community (primarily because of the
> learning curve and lack of core API documentation). Creating
> additional complexities (i.e., needing to comprehend the $(...)
> scoping issue) will simply discourage yet more people from even
> attempting to learn how to write new things for TiddlyWiki.
Again I see this a quite different issue. One could argue that by
making TiddlyWiki more open and friendly to jQuery plugins and
developers we'll expand the TiddlyWiki community of plugin authors.
I suggest we continue to use the jquery alias, build more tests for
"$" and consider flipping to "$" once we understand the problem
better. I think at this point adding TiddlyWiki special "jq" alias
will make matters worse, and only add yet another thing we are
committed to maintain for backwards compatibility.
> Paul wrote:
>> I do feel we may be being overly cautious, and if there is scope
>> leakage then it will help refactoring and plugin authors if we
>> understand the problem better and have regression tests to follow.
>
> Unfortunately, BT/Osmosoft's track record on testing is *abysmal*
Um, I'd like to separate "BT/Osmosoft" from "core developers". Whilst
the people may overlap as in set theory, they are very distinct things
- for example I'm Osmosoft but am not a core contributor.
It is also true that coverage of the current core is pretty abysmal.
We don't have many tests, and code has been added to the core without
tests, but that doesn't prevent writing tests from being the answer.
I'd also note it is hard to write tests for a large code base which
runs in many environments retrospectively and whilst we have
periodically had splurges of writing tests, we do need to carry on
with such initiatives and welcome contributions from others following
the pattern laid out in the core test area - yet another area where
jQuery and qunit is helping us move forward.
If you really feel current TiddlyWiki core testing is abysmal, or
following the wrong approach, then I'd encourage you and others to
show us by examples for plugins and in contributions to the core how
to write better tests.
Best,