$ function

3 views
Skip to first unread message

Tobias Beer

unread,
Jun 11, 2008, 7:45:30 AM6/11/08
to TiddlyWiki
Hi is there a reason why the $ function is not implemented in tw core,
the one that replaces...

var myElem = document.getElementById('objID');

with...

var obj = $('objID') ;

to even use...

var objs = $('obj1ID','ob2ID',obj3); //get an array of the objects
corresponding to the input

just like described here... http://www.dustindiaz.com/top-ten-javascript


Would that make sense / simplyfy the code?

I find it kind of neat to write something like...

if ($('sidebar').style.display!='none') runCode(); //assuming there is
an element with the ID sidebar and run code depending on whether it's
disply style is 'none'

FND

unread,
Jun 11, 2008, 7:52:41 AM6/11/08
to Tiddl...@googlegroups.com
N.B.:
Since this is a strictly development-related question, you'd be better
served on the TiddlyWiki developers group:
http://groups.google.com/group/TiddlyWikiDev/

> is there a reason why the $ function is not implemented in tw core

We've decided that introducing this function is ultimately not
desirable, mainly because it's merely a shortcut/redirection - see the
ticket for details:
http://trac.tiddlywiki.org/ticket/451


-- F.

Tobias Beer

unread,
Jun 11, 2008, 10:14:30 AM6/11/08
to TiddlyWiki
I guess I will join that group...

added a comment to the (unfortunately?!?) "closed" ticket in trac:
http://trac.tiddlywiki.org/ticket/451#comment:7

FND

unread,
Jun 11, 2008, 10:17:14 AM6/11/08
to Tiddl...@googlegroups.com
> I guess I will join that group...

Please do.
We generally prefer to discuss such issues on the groups rather than
adding an excessive amount of comments to tickets directly - so feel
free to start a new thread on [twdev].


-- F.

Eric Shulman

unread,
Jun 11, 2008, 10:26:47 AM6/11/08
to TiddlyWiki
> Hi is there a reason why the $ function is not implemented in tw core,
> the one that replaces...
> var obj = document.getElementById('objID');
> with...
> var obj = $('objID') ;
> just like described here... http://www.dustindiaz.com/top-ten-javascript
> Would that make sense / simplyfy the code?

Although the $(...) function could nominally reduce the core code size
by about 2K, it is just a form of "syntactic sugar" that doesn't
really add any new functionality... and it tends to obscure the
meaning of the core code for those who are not familiar with this
particular idiom.

Nonetheless, it is a fairly clever shorthand that *some* javascript
coders are familiar with and are fond of using. Although it is not
provided by the TW core, the $(...) syntax can prove useful for
reducing code size when writing your own custom inline scripts. To
that end, I've just added a definition for it within
InlineJavascriptPlugin.

Get the update (v1.9.3) here:
http://www.TiddlyTools.com/#InlineJavascriptPlugin

Note: while this function can also be used from within other plugins
(as long as they are loaded after InlineJavascriptPlugin), it is
recommended that you explicitly define this function in any plugins
you create, in order to eliminate any external dependencies.

enjoy,
-e

Tobias Beer

unread,
Jun 11, 2008, 4:10:20 PM6/11/08
to TiddlyWiki
>I've just added a definition for it within InlineJavascriptPlugin
Thanks, you are rather quick at hand ;-)

>as long as they are loaded after InlineJavascriptPlugin...

Newbe-Question:How would I make sure that that is the case?
Give my plugin/macro a name>="InlineJavascriptPlugin"?

Eric Shulman

unread,
Jun 11, 2008, 4:44:47 PM6/11/08
to TiddlyWiki
> >I've just added a definition for it within InlineJavascriptPlugin
> >Get the update (v1.9.3) here:http://www.TiddlyTools.com/#InlineJavascriptPlugin

> >as long as they are loaded after InlineJavascriptPlugin...
> Newbe-Question:How would I make sure that that is the case?
> Give my plugin/macro a name>="InlineJavascriptPlugin"?

Tiddlers tagged with 'systemConfig' (i.e., "plugins") are processed in
alphabetic order by tiddler title when the document is first loaded.
Thus, to ensure that a given plugin is processed after
InlineJavascriptPlugin, it would need to be named so that it occurs
after that plugin.

However: rather than relying upon InlineJavascriptPlugin to define the
$(...) function, it would be even better to simply copy that function
definition into your own plugin code, so that your plugin does not
depend upon InlineJavascriptPlugin, and can therefore be imported and
installed separately into any document.

In contrast, any tiddlers containing inline scripts (i.e., using the
<script>...</script> syntax to embed code *within* other wiki-
formatted tiddler content) are processed *after* all the plugins have
been loaded. Thus, these tiddler may have any title you like, and
will still be processed after InlineJavascriptPlugin has defined the $
(...) function.

-e

Tobias Beer

unread,
Jun 11, 2008, 6:03:27 PM6/11/08
to TiddlyWiki
Sounds great... generally speaking, stuff depends on a lot of other
stuff. So dependencies are fine with me, as long as you know about
them and (are able to) manage them properly. For my purposes, the
InlineJavascriptPlugin works just great, thanks Eric.
Reply all
Reply to author
Forward
0 new messages