Message= vs action-sendmessage

(Windows 10, Firefox 98.0.2, version 5.2.2, Timimi)

<$button message="tm-new-tiddler">Click!</$button>
<hr>
<$button><$action-sendmessage $message="tm-new-tiddler" />Click!</$button>

What’s the difference between the two and when to use them? They both have the same result.

There’s actually 3 ways to invoke tm-new-tiddler from the button. You can also use the actions attribute.

Here’s code that uses all the methods at once.

<$button message="tm-new-tiddler" field2="outer" 
actions="""<$action-sendmessage $message="tm-new-tiddler" field2="actions" />"""
>Click!
<$action-sendmessage $message="tm-new-tiddler" field2="inner" />
</$button>

As you can see when you try it, the 3 methods are invoked in a different order. Also, when you invoking from the button you can’t specify additional fields, including the title field.

I believe the current recommendation is to always use the actions attribute, so that actions will occur in an expected order. For convenience, the actions attribute can refer to a macro that contains the actual code to be invoked.

I think you meant to make a button that makes three tiddlers with each having a field named "field2’ and the values outer, actions, inner. However, my version of TiddlyWiki made 3 tiddlers, with only the last two having a field (actions; inner). Was that to be expected because of the order of the instruction?

What does “invoke from the button” mean? – OK I think that means to run the command when pressed. No, wait. then your second sentence doesn’t make sense. Does it mean the command that’s between the carets that hold the term “button”?

I mean the version that’s inside the button widget. That is, if you wrote just:

<$button message="tm-new-tiddler" field2="outer" >Click!</$button>

It didn’t create a field, because those values can’t be passed when invoked that way. So that’s another argument for not invoking the message in that manner.

The button-widget is one of the very first widgets, that where created, when TW was developed.

During the dev process the widget got more and more parameters for more and more functions, that where needed by TiddlyWiki itself and also by users. So it got a bit messy. …

As always, it depends on your actual usecase. If you “only” want to create a new tiddler as in your OP, it’s OK to use <$button message="tm-new-tiddler">Click!</$button> … It’s readable, it works and everyone should understand what’s going on.

But as soon as you want to do more complex actions and several of them, the best way is as follows:

\define myActions()
<$action-xxx ... />
<$action-yyy ... />
<$action-zzz ... />
\end

<$button actions=<<myActions>> >Click!</$button>

If action-widgets are used inside the button body. It’s not always guaranteed, that all variables used by the action-widgets are updated in the right way. Variables inside the button widget are evaluated, when the button widget is rendered.

I think, the format above is “best practice”, since all variables are evaluated when the button is clicked.

Just my thoughts.

1 Like

@pmario are you referring here to the multiple actions in the button body, I suspect you mean the “actions=” method?

@Farouche

Message= vs action-sendmessage
The use of the button message parameters is great to create simple action buttons. However the action send message can be used in the button body or in a macro passed to the actions=<<actions-macro>> this allows more sophisticated and conditional actions, as do actions in the button body.

The advantage of both the button message or actions in the button body is the code is in one place and thus easier to read and self documenting. I believe the intention is/was to deprecate the actions in the body (I hope this does not happen).

Having the message/actions and body as alternatives can be very helpful depending on the button, the reusability of the actions and refresh sensitive buttons. If I felt more confident I would publish an ordered table indicating these three approachs and their qualities.

Please see this for important information https://tiddlywiki.com/#ActionWidgets for Action Execution Modes, which gives the possibility of also getting the actions inside an actions=macro to also refresh/update each button click.

Yes. I’d consider the code that I showed as “best practice”, for many cases.

1 Like

As I wrote. Actions in the body are not reliable.

1 Like

If there really is a compelling reason to avoid a macro, the following is equivalent:

<$button actions="""
	<$action-xxx ... />
	<$action-yyy ... />
	<$action-zzz ... />
	"""
>Click!</$button>

Action widgets in the body of a button have a performance impact on render and refresh as well.

2 Likes