Custom Code Documentation
Is there any more documentation around what functions /features are accessible via custom code actions or formulas ? I 'm assuming this would be provided by the ctx parameter provided to the custom code . I 've seen these pages already : https://toddle.dev/docs/custom-actions https://toddle.dev/docs/custom-code-api But is there any more ? Can we modify a Toddle variable value from custom code ? Can I call another Toddle formula or action ? Is there any way to debug and /or step through code or formulas to see what they are doing ? Also , is there a way to provide format /type information for object parameters so autocomplete can suggest the properties ? You can use triggerActionEvent to pass data back and update a variable with it . Custom actions are independent so to access another formula you would have to tie to global scope which isn ’t best practice . Code that needs to interact with each other can live in the same action or use arguments /events to use different ones You can debug by logging to console same as anywhere else What about using a custom code formula instead of an action ? Is the return value the output of the formula ? If I create a custom code formula with an input and provide a Toddle variable as the input , if I make changes to this object are those reflected in the original Toddle variable ? Or do I have to output the new value and then set the Toddle variable value to this output ? In coding terms , I guess I 'm asking if the arguments are being passed by reference or by value to the custom code . Custom actions need to be triggered any time you want to run them You need to use triggerActionEvent to pass the data back 👍1Ok I 'm a little confused by this . I tried to setup a [simple test project ] ( https://toddle.dev/projects/custom_code_test/branches/start/components/HomePage ) by having the page Lifecycle On load event set some Toddle variable values . First doing things via a custom code formula and then via a custom code action . I setup the On load event with a workflow to call my custom formula to set the variable values and that works fine . I 'm able to both update the variable value inside the code and also get the return value as the output and use that 👍 . Then I set up the same workflow to call the custom action afterwards to change the variable value again . It seems like the custom action code does nothing unless I also define an event on the action , call that event in the code using triggerActionEvent() and also setup a workflow in response to that event to set one of the variable values . . . is this right ? What confuses me is it seems to be setting both variable values in this case . I 'm expecting the output to be the First Variable value is "Set by custom formula" since I thought I couldn 't manipulate a Toddle variable inside a custom action . . . but it seems to also be updating the variable with any changes I make to it in the custom action code . triggerActionEvent is used like : triggerActionEvent('eventName', data); data is what is being sent back to the editor in the event Which is interesting because you 're not setting it directly Hmm I have this line in my custom formula that does that : args.firstVariable.prop = "Set by custom formula" which is why I expected that to be the output . But then just to test I added the line in the action to also change it with args.firstVariable.prop = "Set by custom action" and most of the time that 's the value I see when I refresh and check the current value of First Variable .prop Hadn 't tried it either That part isn 't the weird part though , it 's that neither action is directly setting the first variable Not sure if to consider it a bug So like i still have it set to call my custom action in On load but it doesn 't seem to do anything anymore First variable was never being set by either action so it should never have changed , it should have kept its initial value prop : null I guess I should clarify . I was setting the prop value inside the first variable object with this line in the formula : args.firstVariable.prop = "Set by custom formula" So that shouldn 't even work your saying ? Like I can 't update a property value of a Toddle variable I pass in as an argument ? Formulas would typically be written to output a value which you 'd use to set the item it is being used in Does that make sense You 're writing it to just change the input argument Yea I think so . I was basically trying to test where I could modify multiple variables at once . Second Variable would contain the output /return value of the formula or action . And inside the custom code for both the formula and action I would be manipulating the properties inside First Variable . It 's more like regular coding so I get it but it 's not how custom code is typically used in toddle I guess Where you use the formula to set something But it also affecting first variable might be a bug but I 'm not sure . Would have to run it with the team I definitely like the option of being able to take a Toddle variable as an argument to custom code and actually modify the variable without needing to return it and then set it in Toddle . Allows me to do more work in 1 custom code call and work on multiple variables at once . Honestly I 'd actually like to just be able to call custom code that doesn 't have a return value and just does some stuff which updates properties on variables in Toddle . I thought that 's what an action would do but not sure how to implement that since it seems like my action only runs if I have an event associated with it , call triggerActionEvent() and then have a workflow which does something with that action . . . otherwise when it 's just like this with no workflow actually handling the event defined on my custom action it doesn 't seem to run my custom code and update the First Variable prop value with this line : args.firstVariable.prop = "Set by custom action" And they work Given how the custom formula is behaving , I would assume you would be able to do the same with a custom action if you change it to a regular return instead of triggerActionEvent Ultimately , I was just trying to figure out a way to implement the [question I posted earlier ] ( https://discord.com/channels/972416966683926538/1329575603262722112/1329575603262722112 ) where I 've got 2 nested arrays and I 'm trying to take all the values from the 2nd array and replace the specified index in the first array with each element from the 2nd array . . . was having a hard time doing it with a visual formula so I thought it would be pretty simple in custom code and was having issues there too but I think I may be able to do it with a custom formula now and just return the updated value and set the Toddle variable to that . @KillerK009
Ultimately , I was just trying to figure out a way to implement the [question I posted earlier ] ( https://discord.com/channels/972416966683926538/1329575603262722112/1329575603262722112 ) where I 've got 2 nested arrays and I 'm trying to take all the values from the 2nd array and replace the specified index in the first array with each element from the 2nd array . . . was having a hard time doing it with a visual formula so I thought it would be pretty simple in custom code and was having issues there too but I think I may be able to do it with a custom formula now and just return the updated value and set the Toddle variable to that . I am going to run it by the team and verify if this is a bug or not