help desk
How to use the 'pre' and 'post' feature of the model package
You can pass a function name as "pre_process" or "post_process" to execute a function before or after the model package function call.
- 'pre_process' processing is available to the `set` and `create` functions
- 'post_process' processing is available to the `get`, `get_from`, `set`, `create`, `list` functions and is executed at the end of the `_get` function.
Ex pre processing:
function set_region(fields) // Make sure a region is set. By default set as the location's region. if trim(fields.region) eq "" then // Get location location = my_location::get(fields.uid_location); if location.type() eq "sed_error::Error" then return location; endif fields.region = location.region; endif return fields; endf fields = {...}; entry = my_package::set(uid, fields, pre_process: "set_region");
Ex post processing:
function get_status(entry) // Define a status from existing conditions entry.status = "new"; if trim(entry.sn_mdate) ne "" then entry.status = "modified"; endif return entry; endf entry = my_package::get(uid, post_process: "get_status");
Ex post with list
function get_status(entry) // Define a status from existing conditions entry.status = "new"; if trim(entry.sn_mdate) ne "" then entry.status = "modified"; endif return entry; endf entries = my_package::list(uid: uid, options: { "post_process": "get_status" });
The function used in 'pre_process' or 'post_process' can be in a package supercharge. When adding your 'pre_process' or 'post_process' parameter to your model package call, simply add the "package::" portion
Ex:
include "model/my_package.sn" // Package supercharge package my_package [...] function get_status(entry) [...] return entry; endf endp // In your resource or SPA page entry = my_package::get(uid, post_process: "my_package::get_status");
It is ill advise to supercharge any of the native model package function. If you do so, improvments could be made to the model functions and your function supercharge could become incompatible (often silently) and could result in bugs hard to discover. It is however sometimes needed to perform tasks or follow business rules before or after certain model function calls. That is where the 'pre_process' and 'post_process' could help you.
Let say that after creating an entry in table 'my_package' your business rule indicate you to always need add an entry in another table. Here is an example:
include "model/my_package.sn" // Package supercharge package my_package [...] function post_create(entry) // Add event in my_event table fields = { ..., uid_reference: entry.uid }; event = my_event::create(fields); if event.type() eq "sed_error::Error" then return event; endif // Do more stuff your business rule ask you to implement return entry; endf endp // In your resource or SPA page fields = {...}; entry = my_package::create(fields, post_process: "my_package::post_create");
.
Répondu le : 0000-00-00 00:00:00