What is the simplest way to store JavaScript output value into a field?

59 views
Skip to first unread message

Pekka

unread,
May 7, 2021, 8:28:00 AM5/7/21
to TiddlyWiki
I have tried to search for answers to this questions but most of them were years old and seemed rather complex. It seems to me that by now there would exist a simple solution.

I have a simple JavaScript code that takes a number representing an interval and calculates the current date + interval. The below code will output the next date correctly in a tiddler text field but I am unable to plug this value into a tiddler field.  

---

\define getNextDate()
<$macrocall $name="next-date" interval=5 />
\end

<<getNextDate>>

---

I have tried the below method to store the output into a field. The confusing part is that both the <<date>> and <<getNextDate>> work fine in the text field and even though <<date>> outputs the date in the text field, the value that is set to the field "due" will be just "<$macrocall $name="next-date" interval=5 />".

<$set name="date" value=<<getNextDate>>>
<<date>>
<$button>
<$action-setfield $field="due" $value=<<date>>/>

Set interval

</$button>
</$set>

Any help would be greatly appreciated!

Pekka

unread,
May 7, 2021, 8:57:46 AM5/7/21
to TiddlyWiki
Thanks to Eric Shulman's reply to jn.pierr.. question in "chaining jobs", I was able to fix the issue with the following changes: 

\define button_actions() <$action-setfield $tiddler=<<currentTiddler>> due=<<next-date 10>> />
<$button actions=<<button_actions>>> Set Interval
</$button>

Now the only thing left to do is to figure out how to set the interval based on field values.

Pekka

unread,
May 7, 2021, 1:26:34 PM5/7/21
to TiddlyWiki
Ok I have solved it for now at least for my purposes, although I feel the solution is far from elegant. I will post and explain the solution for anyone who might be interested.

A dropdown menu is used to select an interval which is added to the current date. We want this new date to be stored in the field "due". To do this, we change the Tiddler fields within JavaScript by using $tw.wiki.setText. This is implemented in the next-date.js and is "invoked" by <<getDate>>. 

For some reason, if <<getDate>> is continuously visible in the text field then everything freezes. So we need to find a way to invoke the JavaScript temporarily. This can be achieved, albeit clumsily, using <$reveal> widget detailed here: https://tiddlywiki.com/static/RevealWidget.html. After invoking the script by showing <<getDate>>, we have to hide <<getDate>> again to prevent the freezing, and this is done by clicking the reveal button again. Due to the freezing the button must be clicked several times until "Restore Functionality" is switched back to "Set Interval".

---

\define getDate()
<$macrocall $name="next-date" title={{!!title}} interval={{!!next_interval}}/>
\end

<$select tiddler={{!!title}} field='next_interval' default={{next_interval}}>
<$list filter='[range[100]]'>
<option><$view field='title'/></option>
</$list>
</$select>

<$reveal type="nomatch" state="$:/state/SampleReveal2" text="show">
<$button set="$:/state/SampleReveal2" setTo="show">Set Interval </$button>
</$reveal>
<$reveal type="match" state="$:/state/SampleReveal2" text="show">
<$button set="$:/state/SampleReveal2" setTo="hide">Restore Functionality</$button>
<<getDate>>
</$reveal>
Reply all
Reply to author
Forward
0 new messages