Scope of $tw and this.wiki (for starter :o) )

109 views
Skip to first unread message

Alain Dutech

unread,
Mar 20, 2016, 1:16:22 PM3/20/16
to TiddlyWikiDev

Hi,
this must be a bunch of stupid questions but I'm still learning the logic of javascript.

1) when is the global $tw variable available?
  - what "module" are required for it to be available ?
  - does it become available after  `require("$:/core/modules/widgets/widget.js");` ? Why ?
  - how come $tw can be used in the filter.js module ? as show below

exports.getFilterOperators = function() {
   
if(!this.filterOperators) {
        $tw
.Wiki.prototype.filterOperators = {};
        $tw
.modules.applyMethods("filteroperator",this.filterOperators);
   
}
   
return this.filterOperators;
};

Is it because getFilterOperators is defined inside "exports" ??
 
2) To use the filter API, and in particular the filterTiddler function defined in filters.js (see below), must I "require" filter.js, or widget.js, or wiki.js or ???

exports
.filterTiddlers = function(filterString,widget,source) {
   
var fn = this.compileFilter(filterString);
   
return fn.call(this,source,widget);
};

3) the `this.wiki` of Widgets is initialized through the option parameters in the Widget constructor.
so all the function exported by wiki.js are available through this.wiki ?

4) Is there like a list of "public" function available through $tw, $tw.utils and this.wiki ? I am trying to guess by reading through the various *.js modules, but I'm my knowledge of JS make this task a bit tedious...

Thanks for any hint or explanations or info.
Alain

BJ

unread,
Mar 20, 2016, 3:22:08 PM3/20/16
to TiddlyWikiDev
HI Alain,
the modular structure of tiddlywiki is more extensive that 'require'.

There is some documentation here

tiddlywiki.com/dev.

but mostly you can read the code! but ask questions when you get stuck.

The $tw structure is populated in a number of step during the start up. It is defined at the begin of the boot and some basic functionality is added to it in boot.js. Alot of extra stuff in added to it in

$:/core/modules/startup/load-modules.js

javascipt modules in tiddlywiki can have a module-type that allow them to be  add to $tw thru $tw.modules.applyMethods(). (including the filter functions)

Some places use 'this' instead of $tw, may be for some future expansion of tw?

all the best

BJ

Alain Dutech

unread,
Mar 22, 2016, 2:22:32 PM3/22/16
to tiddly...@googlegroups.com
Thanks BJ.

I have looked a bit at boot.js and various startup modules. It is a bit more clear (especially how $tw is created and populated, but not perfrect. Still, I cannot say when the $tw can be used.

Specifically, if a create a new module with the classical
`(function(){ ... `

I will not be able to use `$tw.utils`. What should I do to "see" $tw ?
If I call my module from a Widget, I could pass my module the $tw variable ? Other ways ?

And, to go back to filter.js
For example, $tw is used inside the fonction
exports.compileFilter = function(filterString) {

Is it because filters.js is loaded through $tw.modules.applyMethods() that $tw is in its scope ?

Alain



--
You received this message because you are subscribed to a topic in the Google Groups "TiddlyWikiDev" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/tiddlywikidev/Ul3ezJU98QA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to tiddlywikide...@googlegroups.com.
To post to this group, send email to tiddly...@googlegroups.com.
Visit this group at https://groups.google.com/group/tiddlywikidev.
To view this discussion on the web visit https://groups.google.com/d/msgid/tiddlywikidev/5fcc3488-48af-4159-b267-f3a62fcf8a69%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

BJ

unread,
Mar 22, 2016, 4:32:50 PM3/22/16
to TiddlyWikiDev
It depends on what you plugin does. In each tiddler you  need to put what type of module the javascript is so that tiddlywiki can load it at the correct time - generally try and copy what others have done.

$tw is alway visible - it is a global variable.

If you have a particular functionality then I may be able to be more specific, you can experiment and see what works.

Alain Dutech

unread,
Mar 22, 2016, 7:39:12 PM3/22/16
to tiddly...@googlegroups.com
Thanks, it's clearer now.
In fact, it's a stupid error that led me to wonder about $tw.
While testing about it in my module, I tried
console.log("__INIT: $tw="+$tw );
and I had an error, and assumed wrongly I could not "see" $tw. But the error was simply that $tw could not be cast into a string (as asked by the '+'). If I write
console.log("__INIT: $tw=",$tw );
it works....

So I got confused, for no real reason. All my fault !!!

Alain

Jeremy Ruston

unread,
Mar 26, 2016, 2:32:47 PM3/26/16
to TiddlyWikiDev
HI Alain

TiddlyWiki uses it’s own CommonJS compatible module loader. The $tw global is explicitly exposed to loaded modules:


$tw is also exposed to the JavaScript global namespace, but that is not directly visible to TW modules.

Best wishes

Jeremy.

You received this message because you are subscribed to the Google Groups "TiddlyWikiDev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywikide...@googlegroups.com.

To post to this group, send email to tiddly...@googlegroups.com.
Visit this group at https://groups.google.com/group/tiddlywikidev.

Alain Dutech

unread,
Mar 28, 2016, 12:16:16 PM3/28/16
to tiddly...@googlegroups.com
Ok, thank you very much.
Alain

Reply all
Reply to author
Forward
0 new messages