a bug with the tabs macro calling javascript macro ?

61 views
Skip to first unread message

vpl

unread,
Aug 24, 2019, 11:04:37 AM8/24/19
to TiddlyWiki
Hi,

I've created a basic javascript  macro that look for a field ('categfield') from a tiddler ('Test-tiddler')
This macro is called from the 'Test-tiddler'  that allows setting the 'categfield" with a $select

Here is the 'Test-tiddler'

By Category:
<$select field="categfield">
<option value="Categ-1">Categ-1</option>
<option value="Categ-2">Categ-2</option>
<option value="Categ-3">Categ-3</option>
</$select>

<<testmacro>>

When I open 'Test-tiddler' and select a categfield the console.log in the testmacro.js displays correctly the categfield value. Fine

Now if I create a 'test-tab' tiddler like

<<tabs "Tab1 Test-tiddler "Tab1" "$:/state/tab1" "tc-vertical">>

And access the ''Test-tiddler' from there, the categfield are not update when I change it from the select !!

I've tried also to leave opened only the 'test-tab'. Same result

I suspect a bug, don't you think ?

Here is the macro

/*\
title: $:/plugins/vpl/testmacro.js
type: application/javascript
module-type: macro

\*/
(function(){

/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";


/*
Information about this macro

*/

exports.name = "testmacro";

exports.params = [];

/*
Run the macro
*/
exports.run = function() {
console.log("testmacro 0.1");
var categFilterTiddlerPath = 'Test-tiddler';
var cateFIlterTiddler = this.wiki.getTiddler(categFilterTiddlerPath);
var categField = cateFIlterTiddler.getFieldString("categfield");
console.log("Vield Value from macro:"+categField);
return ("Hello");
};

})();

Regards

Vpl

Eric Shulman

unread,
Aug 24, 2019, 11:50:15 AM8/24/19
to TiddlyWiki
On Saturday, August 24, 2019 at 8:04:37 AM UTC-7, vpl wrote:
I've created a basic javascript  macro that look for a field ('categfield') from a tiddler ('Test-tiddler')
This macro is called from the 'Test-tiddler'  that allows setting the 'categfield" with a $select
Here is the 'Test-tiddler'
By Category:
<$select field="categfield">
<option value="Categ-1">Categ-1</option>
<option value="Categ-2">Categ-2</option>
<option value="Categ-3">Categ-3</option>
</$select>
<<testmacro>>
When I open 'Test-tiddler' and select a categfield the console.log in the testmacro.js displays correctly the categfield value. Fine
Now if I create a 'test-tab' tiddler like
<<tabs "Tab1 Test-tiddler "Tab1" "$:/state/tab1" "tc-vertical">>
And access the ''Test-tiddler' from there, the categfield are not update when I change it from the select !!

The $select widget operates on the "currentTiddler" unless you specify the target tiddler (i.e., using tiddler="..." as a param).

Thus, your initial test works, because the $select widget and the <<testmacro>> invocation are both in the same tiddler, "Test-tiddler", which you are viewing directly.

However...

When a tiddler is shown via the tabs macro, the <<currentTiddler>> is the tiddler *in which the tabs macro occurs* (i.e., "test-tab").

Thus, your second test fails, because the $select widget is saving it's value into "test-tab", rather than "Test-tiddler".

To use $select correctly, you can explictly specify the target tiddler using <$select ... tiddler="Test-tiddler">.

Of course, this is probably not very useful, as the target is hard-coded, and I suspect you're end-goal is to be able to save the selected value in the currentTiddler, regardless of what tiddler that is.

Fortunately, there is a much more flexible way to solve the problem:

The <<tabs>> macro sets another "core" variable: <<currentTab>>, which is available *within each tab* that is shown.

To make your content use that target instead of the <<currentTiddler>> you can wrap it within a $tiddler widget, like this:

<$tiddler tiddler=<<currentTab>>>


By Category:
<$select field="categfield">
<option value="Categ-1">Categ-1</option>
<option value="Categ-2">Categ-2</option>
<option value="Categ-3">Categ-3</option>
</$select>
<
<testmacro>
>

</$tiddler>

When the above is viewed *directly*, the value of <<currentTab>> is undefined, and the $select operates on <<currentTiddler>>, just as your initial test does.

When viewed via a <<tabs>> macro, the value of <<currentTab>> *is* defined and the <<currentTiddler>> is then set by the $tiddler widget to match, so that the $select will still operate on the desired tiddler.

I hope this explanation isn't too confusing...

enjoy,
-e
Eric Shulman
TiddlyTools: "Small Tools for Big Ideas!" (tm) http://tiddlytools.github.io (TWClassic)
InsideTiddlyWiki: The Missing Manuals



vpl

unread,
Aug 24, 2019, 12:52:12 PM8/24/19
to TiddlyWiki
Hi,

Precise, accurate & efficient ... No other words to thanks you for this so quick answer which perfectly works !!!!

Thanks very much for your help

Regards

Vpl

PMario

unread,
Aug 25, 2019, 4:44:16 AM8/25/19
to TiddlyWiki
Hi,
Also see: https://tiddlywiki.com/#tabs%20Macro ... the last paragraph.
-m
Reply all
Reply to author
Forward
0 new messages