Announcement

Collapse
No announcement yet.

How to define a global function

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to define a global function

    Hi,

    Like to know if it is possible to define global function such as 'Perform Database Query'.

    I have common logic which may be used by multiple channels.

    If it is possible, how to define it? We will have more then one variable to pass into that function.

    Thanks,
    Charles

  • #2
    Re: How to define a global function

    Charles,

    Javascript is amazingly functional and you can actually store functions in the globalMap. That being said, we have not tested how Mirth will behave when the function is called, but I will try to work on an example for you soon.

    -Chris
    Chris Lang

    Comment


    • #3
      Re: How to define a global function

      global and local variables are saved as strings, so you'll need to use the eval function first to use themes.

      Comment


      • #4
        Re: How to define a global function

        Hi,

        Not sure what you mean. Could you mind give me a short example to define java script inside mapper if I understood it right.

        Thanks

        Charles

        Comment


        • #5
          Re: How to define a global function

          Charles,

          You would create a Javascript step (not mapper).

          Code:
          globalMap.put('add_func', "function add(a, b) {return a+b;}");
          Later on you would do something like

          Code:
          var result =  eval("a=5; b=10;" + globalMap.get("add_func"));
          Result should then be 15.

          We have been planning on adding the ability to have reusable scripts for any channel/destination, however this has been pushed back in priority. Check the JIRA and vote on the issue if you'd like to see it sooner.

          -Chris
          Chris Lang

          Comment


          • #6
            Re: How to define a global function

            I will try it now.

            Thanks a lot.

            Charles

            Comment


            • #7
              Re: How to define a global function

              I found the current way to define global variables and functions is bit of awkward.

              If I define a global variable in a channel, other interface developers cannot easily know the existence of such variables and functions. Therefore, in my opinion, developers shall be able to define global resource outside of channel definition.

              Thanks,

              Charles

              Comment


              • #8
                Re: How to define a global function

                Charles,

                I agree it is awkward. We would love to have a contributor take on this task, as the core development team is currently tied up implementing other changes (EDI/Tcl/etc).

                -Chris
                Chris Lang

                Comment


                • #9
                  Re: How to define a global function



                  By the way, the global function can be used in the next way:

                  Code:
                  eval(''+globalMap.get('add_func'));
                  var sumResult=add(1,2);

                  Comment


                  • #10
                    Re: How to define a global function

                    Ah, I knew there had to be a better way, but I couldn't test it. Thanks Alberto!

                    -Chris
                    Chris Lang

                    Comment

                    Working...
                    X