With 4D 20 R9, 4D introduces new commands that allow developers to define an object context for $4d, ensuring only specific functions are exposed. Previously, using $4d meant exposing 4D methods to the web area, but now, you can define and use class functions instead. This improvement allows for better organization, encapsulation, and security when integrating 4D functionalities into web areas.
Limiting Access to Specific Methods with WA SET CONTEXT
With the new WA SET CONTEXT command, developers can set an object containing only the allowed functions for $4d.
For example, if you set the context of a web area with:
var $myWAObject:=cs.WAFunctions.new()
WA SET CONTEXT ( * ; "MyWebArea"; $myWAObject)
with the WAFunctions class:
Function next()
// Some code here
Function previous()
// Some code here
Function current()
// Some code here
The only $4d javascript functions available in the “MyWebArea” area are:
- $4d.next()
- $4d.previous()
- $4d.current()
In the same way, if you want to allow the use of some existing project methods, you can encapsulate them in an object and pass it to the WA SET CONTEXT command:
var $context:={}
$context.myNextMethod:=Formula(myNextMethod)
$context.myPreviousMethod:=Formula(myPreviousMethod)
$context.myCurrentMethod:=Formula(myCurrentMethod)
WA SET CONTEXT(*; "myWebArea"; $context)
and the only $4d javascript functions available in the “MyWebArea” will be:
- $4d.myNextMethod()
- $4d.myPreviousMethod()
- $4d.myCurrentMethod()
Your Web Pages will run as before, but now you are sure that only those 3 methods can be called.
Conclusion
With this new command, 4D gives you better control over how $4d works in your web areas. You can now limit access to only your chosen functions, making your app more secure and organized. Plus, using class methods with $4d helps keep your code cleaner and easier to manage.
Want to learn more? Check out the official documentation here.