a!save() and saveInto()

anybody can explain a!save() and saveInto() some little more depth

  Discussion posts and replies are publicly visible

  • 0
    Certified Lead Developer

    saveInto is generally a parameter used in user-interactive components that collects an array of save targets to receive the new value entered into that component by a user.  It's not a function hence no "()".

    a!save() is a rule which can be added to within the saveInto list of pretty much any component, allowing incredibly increased functionality by allowing a user to save arbitrary values into arbitrary different variables, including but not limited to a transformed version of the incoming value from the component.

    Here is an example of an a!textField() component that uses a generic save as well as an a!save() inside its saveInto.  I suggest you create a similar interface and play around with it some to really explore how this functionality works.

    load(
      local!textValue: "initial value",
    
      local!textUpdated: false(),
    
      a!textField(
        label: "Test Text",
        instructions: if(local!textUpdated, "(Updated)", ""),
        value: local!textValue,
        saveInto: {
          local!textValue,
          a!save(
            local!textUpdated,
            true()
          )
        }
      )
    )

  • 0
    Certified Lead Developer

    Basically, if you just want to save the value of the component to a variable, you simply type the variable you want in saveInto.

    saveInto: ri!input

    That puts whatever the user chose, typed, selected, etc. into ri!input.  Easy.

    If you need to do something more complicated, or additional things, you can use a!save().  a!save takes a target, the variable the stuff goes into, and a value, the stuff that goes in there.  save!value is a unique variable that you have access to inside that function only, which is whatever the user chose, typed, selected, etc.

    saveInto: a!save(ri!input, save!value) does exactly the same thing as the other code above

    The advantage you get is that you can do all kinds of extra functions on the target, and all kinds of extra functions on the value.  You can, for instance, use an if() to determine which of 3 different variables you save into based on conditions; and at the same time enumerate the value, or sum it with something else, or concatenate a timestamp to the end of it, or whatever you want.  The possibilities are virtually limitless.

    Another fun thing is that saveInto accepts a list, and it will run the list in order from top to bottom.  It's one of only a couple of places where you're guaranteed an order of execution.  One common practice I see is a list with a target at the top and a bunch of saves.  This is what many programmers do when the order doesn't matter, or when it does and it's important that the top one be set first.

    saveInto:{

    ri!input,

    a!save(...),

    a!save(...),

    a!save(...)

    }