Rules can do many things: You can retrieve images from a database, perform math, edit text, perform logic, create lists, create variables or implement functions. Rules implement business logic for the execution and managing of data.
Use the visual rules editor within your application to create business logic for your application. The editor uses the
Google Blockly visual code editor, which is easy to understand when creating native script code. See
Google Blockly Demos to familiarize yourself with
You must have a valid Mobile Development Kit project with existing pages.
The following steps describe how to create a rule visually within your Mobile Development Kit application:
- Right-click the
Rulesfolder and select
New MDK Rule
- Provide a rule name and select
- On the
Use the blocks to create a rule that enforces business logic (do something), create a navigation path, compute field values based on the following categories:
- Logic – if, Boolean
- Loops – do something for a certain count
- Math – simple to advanced math
- Lists – create list, get a list item
- Variables – create and alter variables
- Functions – create functions for reusability
- Text – simple to advanced text manipulation
- Library – contains randomize block
- System – manipulation of data and time
- Client APIs – can query application state and data
- Predefined – where predefined rules are stored
See Customizing Advanced Rules for more details.
When saving your rule, there are two files associated with each rule. One has the extension
.rule and the other has a
rule.js extension. The
.rule file is the visual representation of the code while the
rule.js file, the visual representation,
.rule content does not change. However, if you change your rule within the
.rule file, you overwrite the
Create a Simple Rule¶
This validation rule determines if an editable field is blank when you try to save it. It returns True or False depending on the content of the field. This example displays a message to the user so that it can be fixed. Message actions are not always required.
You have opened the
MDK Rule Editor from Create Rules.
Functionsfrom the left column of the rule editor and select the
if [puzzle piece] return [puzzle piece]. Drag this piece into the editor.
Fill in the first empty puzzle piece to determine what you are comparing:
- Navigate to the
Client APIsmenu on the left side of the editor. Use the puzzle piece that says
clientAPI.bindingand drag it into the first empty puzzle piece.
- There is a pull-down menu next to the word
evaluateTargetPath. Tell the target path where to go by selecting the
Client APIsand navigate to the field to verify if it is blank.
- On the
Object Browserpage, change
UI Propertiesand search for the field that is being validated on the edit page.
- Double-click the field so that the expression is entered in the box on the bottom of the page and select
- Navigate to the
Trueif there is content in that field or
Falseif it is blank. Go to the
Booleansection and drag the
Truepuzzle piece on the same line as the
Falseon the next line.
Warn the user that field is empty by creating a message action. Right-click on the
Actionsfolder and select
New MDK Action.
Template Sectionpage, choose
Message Actionand select
On the new page, provide a name for the action. The type is
Messagebecause you must do something. In this case, go back and fix the problem so the entity can be saved. Provide a friendly message and a title, and OK in the OK caption field. Select
Add the rule and the warning message to the affected page. Find the Update action for the page we edited under the
Actionsfolder. This page contains the action that saves the data, select that action.
Common Action Propertiesand find
Validation Rule. Select the
Object Browsericon on the far right and select the rule that was recently created. Select OK.
Validation Failure Actionand from the
Object Browser, select the new failure action that was recently created. Select
Don't forget to save your project and test your rule!
For advanced usage of rules, see Customizing with Advanced Rules
For use of built-in NativeScript modules, see NativeScript Framework Modules