1- -- ============================================================================
2- -- Nanoflow Examples — client-side flows
3- -- ============================================================================
4- --
5- -- Demonstrates all nanoflow features: validation, navigation, messaging,
6- -- loops, variables, error handling, and return types.
7- --
8- -- Nanoflows run client-side (browser/native mobile). They share microflow
9- -- body syntax but have no transactions, Java actions, or REST calls.
10- --
11- -- Key differences from microflows:
12- -- - No RAISE ERROR / ErrorEvent
13- -- - No Java actions (use CALL JAVASCRIPT ACTION instead)
14- -- - No direct REST/external calls (call a microflow for server work)
15- -- - No binary return type
16- -- - Error handling per-action via ON ERROR, not transactional ROLLBACK
17- -- - SYNCHRONIZE available for offline native mobile contexts
18- --
19- -- ============================================================================
20-
21- -- MARK: Module and entity setup
1+ -- Nanoflow examples — client-side flows
2+ -- Nanoflows share microflow body syntax but restrict server-side actions.
223
4+ -- Setup
235create module NanoflowExamples;
24- create module role NanoflowExamples.User;
25- create module role NanoflowExamples.Admin;
26-
27- /**
28- * Product entity used throughout the nanoflow examples.
29- */
306create entity NanoflowExamples.Product (
31- Name : String(200),
32- Price : Decimal,
33- IsValid : Boolean,
34- Tags : String(500)
7+ Name : String(200),
8+ Price : Decimal,
9+ IsValid : Boolean
3510);
3611
37- -- Helper microflow — server-side save, called from nanoflow examples.
38- create microflow NanoflowExamples.ACT_SaveProduct (
39- $Product : NanoflowExamples.Product
40- )
41- returns Boolean
42- begin
43- commit $Product;
44- return true;
45- end;
46- /
47-
48- -- Helper page — used by N007_OpenProductDetail (requires Mendix 11.0+ page params).
49- create page NanoflowExamples.ProductDetail
50- (
51- params: {
52- $Product: NanoflowExamples.Product
53- },
54- title: 'Product Detail',
55- layout: Atlas_Core.Atlas_Default
56- )
57- {
58- dynamictext text1 (content: 'Product Detail', rendermode: H4)
59- }
60- /
61-
62- -- ============================================================================
63- -- MARK: Nanoflows
64- -- ============================================================================
65-
66- /**
67- * N001: Stand-in nanoflow with no logic.
68- * Used as a placeholder during scaffolding.
69- */
70- create nanoflow NanoflowExamples.N001_Placeholder () begin end;
12+ -- Minimal nanoflow (empty body)
13+ create nanoflow NanoflowExamples.NF_Empty () begin end;
7114
72- /**
73- * N002: Validates a Product before it is saved.
74- * Checks required fields and business rules client-side to avoid a server round-trip.
75- *
76- * @param $Product The product to validate
77- * @returns true if the product passes all validation checks, false otherwise
78- */
79- create nanoflow NanoflowExamples.N002_ValidateProduct (
80- $Product : NanoflowExamples.Product
81- )
82- returns Boolean
83- folder 'Validation'
15+ -- Nanoflow with parameters and return type
16+ create nanoflow NanoflowExamples.NF_ValidateProduct
17+ ($Product : NanoflowExamples.Product)
18+ returns Boolean
19+ folder 'Validation'
8420begin
8521 if $Product/Name = '' then
8622 validation feedback $Product/Name message 'Name is required';
@@ -93,167 +29,51 @@ begin
9329 return true;
9430end;
9531
96- /**
97- * N003: Counts the number of products in a list.
98- * Demonstrates LOOP with BEGIN/END LOOP, DECLARE, and SET.
99- *
100- * @param $Products List of products to count
101- * @returns The number of products in the list
102- */
103- create nanoflow NanoflowExamples.N003_CountProducts (
104- $Products : list of NanoflowExamples.Product
105- )
106- returns Integer
107- folder 'Utilities'
32+ -- Nanoflow calling another nanoflow
33+ create nanoflow NanoflowExamples.NF_SaveProduct
34+ ($Product : NanoflowExamples.Product)
35+ folder 'Actions'
10836begin
109- declare $Count integer = 0;
110- loop $Product in $Products
111- begin
112- set $Count = $Count + 1;
113- end loop;
114- return $Count;
115- end;
116-
117- /**
118- * N004: Creates and returns a new (uncommitted) Product with the given name and price.
119- * Demonstrates creating an entity object and returning it from a nanoflow.
120- *
121- * @param $Name Product name
122- * @param $Price Product price (must be non-negative)
123- * @returns A new Product object (not yet committed to the server)
124- */
125- create nanoflow NanoflowExamples.N004_BuildProduct (
126- $Name : String,
127- $Price : Decimal
128- )
129- returns NanoflowExamples.Product
130- folder 'Factory'
131- begin
132- $Product = create NanoflowExamples.Product (
133- Name = $Name,
134- Price = $Price,
135- IsValid = false
136- );
137- return $Product;
138- end;
139-
140- /**
141- * N005: Shows a status message of the appropriate severity.
142- * Demonstrates SHOW MESSAGE with different type keywords.
143- *
144- * @param $Status Status code: 1 = information, 2 = warning, any other = error
145- */
146- create nanoflow NanoflowExamples.N005_ShowStatusMessage (
147- $Status : Integer
148- )
149- folder 'UI'
150- begin
151- if $Status = 1 then
152- show message 'Operation completed successfully.' type Information;
153- else
154- if $Status = 2 then
155- show message 'Please review your data before continuing.' type Warning;
156- else
157- show message 'An error occurred. Please try again.' type Error;
158- end if;
159- end if;
160- end;
161-
162- /**
163- * N006: Validates and saves a product via a server-side microflow.
164- * Demonstrates calling another nanoflow, calling a microflow,
165- * conditional messaging, and closing the current page on success.
166- *
167- * @param $Product The product to validate and save
168- */
169- create nanoflow NanoflowExamples.N006_SaveProduct (
170- $Product : NanoflowExamples.Product
171- )
172- folder 'Actions'
173- begin
174- -- Client-side validation first (avoids a server round-trip on invalid data)
175- $IsValid = call nanoflow NanoflowExamples.N002_ValidateProduct ($Product = $Product);
176- if not ($IsValid) then
37+ $IsValid = call nanoflow NanoflowExamples.NF_ValidateProduct(Product = $Product);
38+ if not($IsValid) then
17739 return;
17840 end if;
179-
180- -- Mark the product as valid before saving
18141 change $Product (IsValid = true);
182-
183- -- Call the server-side save and show a confirmation
184- $Saved = call microflow NanoflowExamples.ACT_SaveProduct ($Product = $Product);
185-
186- if $Saved then
187- show message 'Product saved successfully.' type Information;
188- close page;
189- else
190- show message 'Could not save the product. Please try again.' type Warning;
191- end if;
42+ log info 'Product validated and saved';
19243end;
19344
194- /**
195- * N007: Opens the product detail page for the given product.
196- * Demonstrates SHOW PAGE with a page parameter.
197- *
198- * @param $Product The product whose detail page to open
199- */
200- create nanoflow NanoflowExamples.N007_OpenProductDetail (
201- $Product : NanoflowExamples.Product
202- )
203- folder 'Navigation'
45+ -- Nanoflow with multiple parameters
46+ create nanoflow NanoflowExamples.NF_FormatPrice
47+ ($Amount : Decimal, $Currency : String)
48+ returns String
49+ folder 'Helpers'
20450begin
205- show page NanoflowExamples.ProductDetail ($Product = $Product );
51+ return $Currency + ' ' + formatDecimal($Amount, 2 );
20652end;
20753
208- /**
209- * N008: Formats a price as a currency string.
210- * Uses CREATE OR MODIFY so repeated execution is idempotent.
211- *
212- * @param $Amount The numeric amount to format
213- * @param $Currency The currency code prefix (e.g. 'USD', 'EUR')
214- * @returns A formatted string like 'EUR 12.50'
215- */
216- create or modify nanoflow NanoflowExamples.N008_FormatPrice (
217- $Amount : Decimal,
218- $Currency : String
219- )
220- returns String
221- folder 'Helpers'
222- begin
223- return $Currency + ' ' + toString($Amount);
224- end;
225-
226- -- ============================================================================
227- -- MARK: Security
228- -- ============================================================================
229-
230- grant execute on nanoflow NanoflowExamples.N002_ValidateProduct to NanoflowExamples.User;
231- grant execute on nanoflow NanoflowExamples.N003_CountProducts to NanoflowExamples.User;
232- grant execute on nanoflow NanoflowExamples.N004_BuildProduct to NanoflowExamples.User;
233- grant execute on nanoflow NanoflowExamples.N005_ShowStatusMessage to NanoflowExamples.User;
234- grant execute on nanoflow NanoflowExamples.N006_SaveProduct to NanoflowExamples.User;
235- grant execute on nanoflow NanoflowExamples.N007_OpenProductDetail to NanoflowExamples.User;
236- grant execute on nanoflow NanoflowExamples.N008_FormatPrice to NanoflowExamples.User, NanoflowExamples.Admin;
237-
238- -- ============================================================================
239- -- MARK: Discovery commands
240- -- ============================================================================
54+ -- Security
55+ grant execute on nanoflow NanoflowExamples.NF_ValidateProduct to NanoflowExamples.User;
56+ grant execute on nanoflow NanoflowExamples.NF_SaveProduct to NanoflowExamples.User;
57+ grant execute on nanoflow NanoflowExamples.NF_FormatPrice to NanoflowExamples.User;
24158
59+ -- Show nanoflows
24260show nanoflows;
24361show nanoflows in NanoflowExamples;
244- describe nanoflow NanoflowExamples.N002_ValidateProduct;
245- show access on nanoflow NanoflowExamples.N002_ValidateProduct;
24662
247- -- ============================================================================
248- -- MARK: Lifecycle — rename, move, drop
249- -- ============================================================================
63+ -- Describe
64+ describe nanoflow NanoflowExamples.NF_ValidateProduct;
65+
66+ -- Rename
67+ rename nanoflow NanoflowExamples.NF_Empty to NF_Placeholder;
68+
69+ -- Move
70+ move nanoflow NanoflowExamples.NF_Placeholder to NanoflowExamples;
25071
251- rename nanoflow NanoflowExamples.N001_Placeholder to N001_Unused;
252- move nanoflow NanoflowExamples.N001_Unused to NanoflowExamples;
253- drop nanoflow NanoflowExamples.N001_Unused;
72+ -- Drop
73+ drop nanoflow NanoflowExamples.NF_Placeholder;
25474
255- -- ============================================================================
256- -- MARK: Access management
257- -- ============================================================================
75+ -- Show access
76+ show access on nanoflow NanoflowExamples.NF_ValidateProduct;
25877
259- revoke execute on nanoflow NanoflowExamples.N002_ValidateProduct from NanoflowExamples.User;
78+ -- Revoke
79+ revoke execute on nanoflow NanoflowExamples.NF_ValidateProduct from NanoflowExamples.User;
0 commit comments