Announcement

Collapse
No announcement yet.

A way to include chunks of code

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

  • A way to include chunks of code

    I have been unable to find a way to take a chunk of code that has to be used multiple times in a study and use an "include" statement to insert it in the locations it is needed. Is there a way to do this in EFS/JavaScript? I have one study that uses a piece of code 28 times, and any time I change a condition I have to make 28 changes.

    Thanks in advance.

  • #2
    Hi werosen,

    Yes, it is very easy in the eSignal JavaScript environment.

    What you do is create a function outside of the premain and main sections of your code. For example, if you were to take the average of two numbers in the function.

    PHP Code:
    function WerosenAverage(a,b){
        if (
    a==null || b==null){//check for nulls first, not required but considered good practice
            
    debugPrintln("Hey, WerosenAverage Function is getting null values");
            return 
    null;
        }
        var 
    tmp = (a+b)/2;
        return 
    tmp;

    Then to call the function, you can call it multiple ways


    1)
    WerosenAverage(1,2);

    2)
    var t = WerosenAverage(1,2);//then the local variable would be equal to the returned average for the remainder of the execution of the main() section, since it is local to main, it cannot be shared outside main, and the next execution of main it would be assigned a new value

    3)
    if you defined variable t as a global variable outside main and premain, you can share it in otehr functions in thecode as well, as it is "globally" defined.

    outside main or premain

    var t;

    then inside main

    t = WerosenAverage(1,2);

    If you have not already, you should check out the eSignal JavaScript resources on this site, as well as the numerous other JavaScript resources on the Web.

    I would start here http://www.esignalcentral.com/university/default.asp

    then migrate towards the "All About JavaScript - The Foundation of EFS" section. There are very good links there with tons of information

    Hope this helps.

    Comment


    • #3
      Not using Function

      I probably wasn't clear enough n my question. I am not using a function that passes values. What I need to include is essentially text that is part of a formula that needs to be executed on every bar. That is why I used the 'include' example. If I were creating a menu for an html page in ASP, I would use an 'include statement' for a text file that contained the text to be included when a page was called. In this manner, changes could be made to a single text file and the new menu would apply to all pages calling this file. I would like to be able to do the same thing in my EFS formula.

      Comment


      • #4
        werosen,

        Sorry, I am not familiar with the include statement you are referring to.

        Also, you indicated that there is text that is part of a formula. Not very clear there, at least to me, so I can only hazard a guess. Try using the eval() command which will take a formula, which is expressed as a text value and execute it like it were written as a formula. btw, the text value could easily be returned from a call to a function as well.

        If that is not it, I would suggest following the links I had previusly provided and checking this thread for further guidance.

        Comment


        • #5
          Hello werosen,

          Things work a little differently in the programming world of EFS compared to the web world. EFS does not have includes. I think what Steve was describing to you is actually what you need to do. If the text you are referring to is a code block that you want to reuse in the various formulas you create and don't want to retype all the code over and over again, you could put that code into a function. You can then call this function with one line of code in your formulas that needs execute that block of code. The function does not have to have any parameters that you pass to it. It can simply execute some actions or calculate a number and return that result back to the formula that called it. The best way to do this is through a Function Library file.

          1) Create a file with a .efsLib extension in the \eSignal\FunctionLibrary\ folder.

          2) surround your code with the function syntax.

          PHP Code:
          function doSomething() {
              
          // paste your code here
              
          return;

          3) In each formula you write that needs to execute this code add the library to your formula in the global scope. Global scope refers to any area in your formula that is outside any user-defined functions such as main() and preMain().

          PHP Code:
          var myLibrary addLibrary("myLibraryFileName.efsLib");

          function 
          main() {
             ...
             ... 
          4) Inside main or any other user-defined function you create in your EFS formula you can now call your doSomething function to execute its code block.

          PHP Code:
          function main() {
              
          // return a value to be assigned to xMyData
              
          var xMyData myLibrary.doSomething();

              
          // or 

              //  just execute an action
              
          myLibrary.doSomething();
              ....
              ....


          If you post your code or "text" that you are referring to, we'll get a better idea of what you are trying to accomplish.
          Jason K.
          Project Manager
          eSignal - an Interactive Data company

          EFS KnowledgeBase
          JavaScript for EFS Video Series
          EFS Beginner Tutorial Series
          EFS Glossary
          Custom EFS Development Policy

          New User Orientation

          Comment

          Working...
          X