4D v16Calling 4D Tables and Methods |
||||||||||
|
4D v16
Calling 4D Tables and Methods
Calling 4D Tables and Methods
The access mode for 4D tables referenced in the Wakanda application depends on how the external catalog is integrated, which is defined in Wakanda during its connection to the remote application (see Integrating with the active model or using a dedicated model):
When merged with the active model, 4D tables referenced in the Wakanda application can be used directly in the Server-side JavaScript code as properties of the ds object, just like local datastore classes. Note: The ds object contains the current datastore of the Wakanda application. For example, to perform a query in the records of the [Employees] 4D table, you can write: var emp = ds.Employees.query("age > :1",30); //retrieve a collection of records from the Employees table //where age is greater than 30 in the emp variable On the client side, you can also take advantage of automatic mechanisms of the datasources based on the datastore classes and associated with the widgets. For example, if you associate the ’employees’ datasource with a ’Grid’ type widget, you can display the list of employees automatically: When the table is associated with a datasource, you can also access its data using this datasource. For example, to sort the collection of records of the ’employees’ datasource, you can write: sources.employees.orderBy("age"); //sorts the collection of employees by their age For more information about working with datastore classes, refer to the Wakanda documentation. The 4D tables referenced are used in the Server-side JavaScript code as properties of the catalog where they were placed when the link was created. The name of this catalog is the one that you passed in the Remote datastore name connection parameter (Wakanda Studio connection dialog box) or in localName (JavaScript methods). For example, if you created a link called "my4Dstore" and want to perform a query among the records of the [Employees] table, you can write: var emp2 = my4Dstore.Employees.query("age > :1", 30); // search in the records of the Employees table // in the link named "my4Dstore" Implementation note: On the client side, using a dedicated model in the current version of 4D Mobile does not allow access to remote classes at the present time. 4D methods referenced in the Wakanda application can be used directly in the JavaScript code as properties of the datastore class, entity collection or entity objects, depending on their scope as defined on the 4D side (see the Parent table and Scope of project methods paragraph). Here is the correspondence between Wakanda objects and the scope of the project methods:
Note: 4D methods can also be called on the client side using datasources (see below); in this case all the methods are available, and the datasource applies them automatically to the current collection or the current entity depending on the context. For example, if you perform a query using the query method (see previous section), Wakanda returns an entity collection. You can execute any 4D project method whose scope is declared as "current selection" on this collection. There are three ways for 4D methods to be called by JavaScript code:
As with standard methods, you can pass parameters during the call, which are received in order in the parameters $1, $2, and so on. Similarly, the method can return a result in the $0 variable. Example: You want to give a 5% raise to employees whose salary is less than 1500.
You can also return a 4D selection directly as a Wakanda collection using the MOBILE Return selection command. For example: //FindCountries project method When calling a 4D method by means of the Wakanda link:
Note that after executing a method through 4D Mobile, the 4D context is reset:
You must make sure the scope of the 4D method corresponds to the type of Wakanda object that is calling it, otherwise a "TypeError: 'undefined' is not a function" error is returned by Wakanda. For example, given the 4D "getcursel" method containing the following code: $0:=Records in selection([Table_1]) Given the run method on the Wakanda side: var tt = ds.Table_1.query("Field_2 = 'a*'").getcursel(); The query( ) method returns a collection. If the scope of the getcursel method was set as "Current record", Wakanda returns the following error:
|
PROPERTIES
Product: 4D
HISTORY
ARTICLE USAGE
4D Mobile ( 4D v16) |
||||||||