So that I don’t need to maintain several copies of the same code stored in multiple actions.
Maybe stored within the action group?
So that I don’t need to maintain several copies of the same code stored in multiple actions.
Maybe stored within the action group?
Several ways to do it, but the recommended way is to put any scripts you want to share in their own actions, and use the Include action action step to insert them into other actions.
Thanks for the quick reply.
Hrmmm. I have some thoughts, but need time to digest this.
Update: Tested this, and it works. But, still, thoughts…
Initial follow-on questions (yes, I think there will be more):
The implication of this is that there’s a single JavaScript context created at the start of an action execution that’s shared across all steps. Are there any larger or smaller lifetimes? I’m just trying to figure out interacting actions and steps.
Just found the answer to my other question. This page could do with some expansion on the environment that code runs in.
I was also interested in this question. Unfortunately all the links given above have rotted since 2018. I found
I could not find the new link for
But I experimented, making an action called Common with one JavaScript step which defined some variables and functions. A second action, with “Include action Common” as the first step, then a second JavaScript step referencing the defined variables and functions, works fine.
You probably want this page in the documentation rather than the GitHub repo.
Thanks. It describes “A single "global" context object” but it doesn’t talk about JavaScript action environment or relationships, as clonezone pointed out.
The context object as noted applies to the whole action - so all steps in the Action share the same context. JavaScript is called via a step and so share the context with other JavaScript and non-JavaScript steps in the action.
The environment for an action is shared across all steps in the action and the context object is part of that underlying environment. You should code any JavaScript with that scope in mind, but you probably want to be coding around limiting variable scope anyway as good practice.
So there is an implication here, but there is nothing explicit on the nature of the environment.
Note, most action creators get away with one script action step that accomplishes all step tasks. The require function provides an excellent option for code development and reuse. But the include action does provide other reuse options and notes that the actions be have as if they are one single action - i.e. shared environment, etc.
Hope that helps.
Thanks for making the implicit explicit! That’s very helpful.
I didn’t know about require. It sounds as if it would be cumbersome to maintain Javascript code in two different places—action steps within the Drafts app, and require files outside it. But is there some way whereby the latter can be managed with a smart JavaScript editor? As far as I can see, Drafts provides no sophisticated support for editing JavaScript in action steps.
Yes. You can find lots of information about that in this thread.