Q:
I put some js functions (f.e. for click or change events) in my WebApp (all in a lib_page1.js file)
In these functions, I need data access and I intend to do it such way: x = rs.fields("fieldname").value
Have I to open the database (var db) and to declare the record set (var rs) in every function?
Or is it possible to have global vars?
And where had I to put the global vars and the openDatabase and openRecordSet commands?
A:
You can access the data in use in your App using 'local data cursors'.
For example, if my App uses the southwind database and the customers table (in Page1.Section1), I don't need to open the southwind database or a customers recordset in my JavaScript event delegates, I can use Lianja.getCursor() and update a value:
Code:
custcurs = Lianja.getCursor("customers");
custcurs.setData("Region","West");
custcurs.update();
custcurs.refresh();
Lianja.get("page1.section1").refresh();
You can also use Lianja.createCursor(database as Character, table as Character) if you need a cursor to a table not currently open in your App.
The Lianja.OData_XXX functions can also be used for custom CRUD operations.
JavaScript variables declared as document.varname can be accessed globally, e.g.:
Code:
document.myvar = "Hello World";
elsewhere in App:
Code:
messageBox(document.myvar);
If you want to access the data currently displayed in the client, you can use Lianja.get("page.section.field").value (or Lianja.get("page.section.field").text) for formitems in a Form Section and you can use name.value or name.text for controls in a Canvas Section (change the name from fieldXX to make sure they have a unique name).
If you want to access the buffered data, use Lianja.getCursor("table") as I mentioned before.
If you want to access a table not currently open in your App, you can use Lianja.createCursor(database as Character, table as Character).
If you want to update data outside the client UI, you can call a server-side program (Lianja/VFP non-UI code) with Lianja.evaluate("myprog()") then requery the local cursor and refresh UI client display if affected.
When building Web Apps in Lianja the norm is to write your client side delegate code in JavaScript and your server side procedures in Lianja/VFP.
In Lianja v2.0 we have implemented a new LianjaWebFramework client side function called Lianja.evaluateJavaScript() which is used for calling server-side JavaScript code.
Note that the Google V8 JavaScript engine is embedded into the Lianja Cloud Server so this is the same JavaScript engine that is used in node.js. This provides you with the ability to write end-to-end JavaScript code on the client and server and generate dynamic pages with .jssp pages also. So if you are a JavaScript guy this is for you.
Code:
// Server: The file myfunction.js should be in the app or library directory and contain a function with the
// same name as the file.
function myfunction(arg)
{
// do something useful...
return arg;
};
// Client: Call a server-side JavaScript function called myfunction from a client delegate
var result = Lianja.evaluateJavaScript("myfunction('hello world')");
Note that if the file myfunction.js file does not exist in the app or library directory and the file server_functions.js exists then the function myfunction is assumed to be defined in the server_functions.js file.
Provide transparent remote function calls to server-side code in Lianja/VFP, JavaScript, PHP or Python by setting up an app.conf file (in the App directory) which exposes the functions that exist on the server and what scripting language that they are written in.
It will provide you with the ability to call functions in your client code that execute seamlessly on the server without having to use Lianja.evaluate() or Lianja.evaluateJavaScript() or Lianja.evaluatePHP() or Lianja.evaluatePython().
You escape string quotes in JavaScript by prefixing them with \ e.g \' or \".
This is standard JavaScript.
I would not recommend this convoluted way of calling functions contained within a library.
I would wrap them by a separate .prg file.
"javascript variable scope".
Lianja.evaluate() needs to be able to access the variable based on where it is declared.
Write your own JavaScript function and experiment with trying to access variables outside of it.
Q:
In my app, I have a canvas section that contains a combo box, textbox and command buttons.
Is it possible to reference the text in the textbox from a command button?
A:
A UI control that is in a javascript canvas section can be referenced using the "id" of the UI control.
So if you have a text field with the id mytextfield you can just reference myfield.text
Lianja v2.0 Beta16 introduces a simplified approach for calling server-side business procedures directly from the JavaScript client.
This is accomplished by defining the libraries, functions and scripting language for the functions in the exports.conf file which can reside in the app and/or the library directory.
Example exports.conf file:
Code:
# This is an example exports.conf file
#
# Each line describes a function that will be available directly from JavaScript code in the client
#
# library, function, type (source library, name of the function, language vfp or javascript)
# (php and python will be added later)
#
# or
#
# library, function (implied type of vfp)
#
# or
#
# function (file expected to exist with a .dbo extension)
#
mylib,myfunc1,vfp
myjslib,myfunc2,javascript
myfunc3,vfp
myfunc4
Now in your App logic running in the Lianja JavaScript Web/Mobile App you can call the server-side functions directly:
Code:
// Note that arguments are automatically converted into the correct format for the
// remote function call and the function call is proxied
// using a synchronous Lianja.evaluate() call
var result = myfunc1("hello world", 2015, true);
var result2 = myfunc2();
var result3 = myfunc3();
// When calling remote functions with objects as parameters the object is automatically
// converted to JSON and send base64 encoded
// This allows you to send and receive complete JSON encoded objects between the
// client and the server
var result4 = myfunc4( {"name":"barry", "company":"Lianja" });
Q:
I would like to change the section header.
I would like to update a canvas label with one of the parsed values.
Code:
Lianja.get("page1.section1.lblName").caption = alltrim(parseddata.name);
How do I refresh the section or the label in the web view/browser to reflect the changed value?
A:
The ID (name) of the UI object in a JavaScript canvas section is a javascript object. So if you have a label called m_label you can set the text using:
Code:
m_label.text = "Hello world";
"m.varname" is a VFP convention, not JavaScript. Try removing the "m.".
Q:
In the section header caption, I add something similar to {m.headercaption}.
In the main .js file, I add a declaration before all code similar to var headercaption = '';
When I want to update the caption, I would set the value headercaption = "some text" and refresh the section.
The header remains as {headercaption}
how I should handle the macros?
In the .js file before any code, the following is used:
Code:
var headercaption = " ";
A:
remove var.
Code:
window.headercaption = "hello world";
Macros work when bound to server-side expressions.
The macros in section headers use Lianja.evaluate().
These don't handle client side javascript variables.
I have implemented support for section captions in the Web/Mobile client now in v2.0. See below.
You can also get/set the caption of a page also in the next v2.0 beta build. This provides you with availability to programmatically change the page and sections headers in JavaScript code.
All topics in [Answers] alphabetically: http://www.lianja.com/community/show...ll=1#post12352
Bookmarks