JS

Q:
Say I have a .prg file set as:

Code:
proc A()
...
endproc

proc B()
...
endproc

proc C()
...
endproc

...

In VFP, if it’s not a library file, I can use something similar to:

Code:
set procedure to some_prgfile
C()
release procedure

How do I call a specific VFP procedure from a .prg file from javascript, such as C()?

I know that if I have a file named code.prg, I can use Code:

Code:
Lianja.execute("do code.prg");

This seems to only work if ‘proc’ is not declared and is to execute all code in the file (registration web app has an example of this).

A:
You could do this:

Code:
//some_prgfile.prg
proc A()
return 'retval from a'
endproc

proc B()
return 'retval from b'
endproc

proc C()
return 'retval from c'
endproc

parameters funcname
return &funcname()
//end

Then in JS:

Code:
var retval = Lianja.evaluate('some_prgfile("c")');

passing further parameters if required by the individual functions.
Here, some_prgfile is the .prg function library, compiled and deployed as some_prgfile.dbo to the Lianja Cloud Server. You pass in the name of the function (here c), which is picked up by the PARAMETERS statement in the function library and the function is called. If the function itself requires parameters, then you pass them in too. PCOUNT() will tell you how many parameters have been passed.
It needs to be in C:\Lianja\cloudserver\tenants\public\wwwroot\apps\ for the Web Client – is it there too?

You should be able to enter your equivalent of

Code:
Lianja.evaluate('some_prgfile("c")')

in the browser console and see what it comes back with.

I would put a check in to test whether the code is running on the desktop or live in the Web Client and just use the {} macro in the Web Client.

This is being called from a commandbutton click. I’m passing the txtsearch.text directly. The [] can be used as string delimiters ‘in dev’ as this is handled by the local Lianja/VFP scripting.

Code:
////////////////////////////////////////////////////////////////
// Event delegate for 'click' event
function page1_section1_field2_click()
{
        if (typeof LianjaAppBuilder === 'object')
        // in dev
        {
        var result = Lianja.evaluate('lib_webtest("get_brnum(['+txtsearch.text+'])")');
        }
        else
        // in live web/cloud
        {
        var result = Lianja.evaluate('lib_webtest("get_brnum(\'{txtsearch.text}\')")');
        }

        Lianja.writeLog(result);
};

In my original post about passing parameters into the library prg, when I mentioned PCOUNT(), I envisaged passing the parameters in individually to the library, rather than including them as a single parameter.

For example, if this is my procedure library, lib_webtest.prg:

Code:
proc get_brnum
parameters ptext
return ptext + " was passed"
endproc

parameters funcname, para1
do case
        case pcount() = 1
                return &funcname()
        case pcount() = 2
                return &funcname(para1)
endcase

I can pass 2 parameters to lib_webtest and it will treat the 2nd as a parameter to the function name in the 1st. Additional parameters can be added and handled with further PCOUNT() cases.

So my button click code becomes:

Code:
////////////////////////////////////////////////////////////////
// Event delegate for 'click' event
function page1_section1_field1_click()
{
        if (typeof LianjaAppBuilder === 'object')
        {
        var result = Lianja.evaluate('lib_webtest("get_brnum","' + txtsearch.text + '")');
        }
        else
        {
        var result = Lianja.evaluate('lib_webtest("get_brnum","{txtsearch.text}")');
        }

        Lianja.writeLog(result);
};

and is probably more readable.



Advertisements
Categories JS

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s