I have two Drafts actions, each with one JavaScript step.
Action Test setTemplate has
draft.setTemplateTag("testTemplate", "xyzzy");
app.queueAction(Action.find("Test getTemplate"), draft);
Action Test getTemplate has
let result = draft.getTemplateTag("testTemplate");
alert(`Test getTemplate got "${result}"`);
I run Test setTemplate on some arbitrary draft. I expect the alert to say
Test getTemplate got "xyzzy"
but in fact it says
Test getTemplate got ""
What am I doing wrong?
That will not work. Each action runs in its own context, and custom template tags are temporary values specific to a context.
When you are queuing an action, you are saying “run this action after the current one finishes” – so, new context.
What are you trying to accomplish? There are certainly other ways to pass information to another action. If you clarify the goal, I could suggest some directions.
1 Like
Okay, thanks. So template tags are properties of the current action object, not the current draft. Perhaps this would be clearer if the calls were like
action.setTemplateTag()
instead of
draft.setTemplateTag()
In the case I’m coding at present I’ve been able to pass the necessary information between the two successive actions by putting it into draft.content. It is a stringified object. But if there are alternatives they might be useful in future.
Still not sure what you are trying to do. If you explained the use case more than the technical problem, it would probably help.
You can, instead of queuing an action, use the Include Action action step, that brings the steps from another action into the current context. Maybe that is what you are looking for…and, within the current context, even multiple script steps share the same global JavaScript context, so you wouldn’t even need to use template tags, because global variable would be shared across steps.
The first action looks at a draft coming from Cat Scratches and decides which of several other actions is required as the next step, then passes data on to one of them. So IncludeAction isn’t ideal for this case. But I can manage by using draft.content for transport.
The action name in the include action step can be a template tag
The action name in the include action step can be a template tag
Hmm, sounds good, I’ll try that out.
within the current context, even multiple script steps share the same global JavaScript context, so you wouldn’t even need to use template tags, because global variable would be shared across steps.
The follow-up action is also sometimes run standalone. So I don’t see how global variables would work, because each variable should be declared exactly once. But template tags plus conditional logic would still handle it.
Your script could just construct a new draft with the content needed, and queue the action on that draft…
let d = new Draft()
d.content = "here's what I actually want to run this on...."
d.update()
app.queueAction(otherAction, d)
Using queueAction with the information passed via a draft does seem the cleanest solution. In my current case a new draft isn’t needed because the original one is disposable, but using a new one is a good idea for more general cases.