π§ AppML β How to Use Controllers (with Full Code Explanations)
π§² Introduction β Why Use Controllers in AppML?
AppML (Application Modeling Language) lets you build data-driven apps using only HTML and model files. But what if you want to add custom logic such as form validation, data filtering, or dynamic user feedback?
Thatβs where AppML controllers come in. Controllers are small JavaScript files that allow you to extend AppMLβs capabilities without needing a large JS framework. They help manage app lifecycle events and provide hooks to control user interactions.
π― In this tutorial, youβll learn:
- What controllers do in AppML
- How to attach a controller to a view
- How to use lifecycle methods (
onready,onvalidate, etc.) - Examples with full code and detailed explanations
π§± What Is an AppML Controller?
An AppML controller is a JavaScript file that defines a set of functions to handle specific events in the AppML application lifecycle.
| Hook/Method | When It Runs |
|---|---|
onready() | When the AppML application is initialized |
onshow() | After data is loaded and rendered |
onvalidate() | Before a form is submitted |
oninsert() | Before inserting a new record |
onupdate() | Before updating an existing record |
onsave() | After a record is saved |
These methods are defined on the global myAppML object and are triggered automatically when the respective event occurs.
π§ͺ Example 1 β Basic Controller with onready and onshow
β
Controller File: controller.js
myAppML.onready = function() {
console.log("AppML is initialized and ready.");
};
myAppML.onshow = function() {
alert("Data has been successfully loaded!");
};
β Explanation:
onready: This logs a message to the browser console once AppML has fully initialized the view.onshow: This displays an alert popup after data is rendered on the page.
β HTML File:
<div
appml-controller="controller.js"
appml-model="models/users-model.json"
appml-data="users.php">
<h2>User Directory</h2>
<p>{{name}} β {{email}}</p>
</div>
βοΈ This <div> connects the AppML model, the server-side data source, and the controller logic to create a dynamic, interactive user list.
π§ͺ Example 2 β Validating Form Input with onvalidate
β
Controller File: validate-controller.js
myAppML.onvalidate = function() {
if (!myAppML.data.email.includes("@")) {
myAppML.message = "Please enter a valid email address.";
return false;
}
return true;
};
β Explanation:
- This code checks if the entered email contains an
@symbol. - If not, it sets a message and blocks the form submission by returning
false. - If valid, it allows the submission (
return true).
β HTML Form:
<div
appml-controller="validate-controller.js"
appml-model="models/form-model.json"
appml-data="submit.php"
appml-message>
<input name="email" placeholder="Enter your email">
<button appml-submit>Submit</button>
</div>
βοΈ This creates a dynamic HTML form that validates email input without writing any JavaScript in the HTML itself.
π§ͺ Example 3 β Filtering Data Dynamically with onshow
β
Controller File: filter-controller.js
myAppML.onshow = function() {
myAppML.filter = function(row) {
return row.status === "active";
};
};
β Explanation:
- This controller filters the displayed data to only show records where
status === "active". - The
filterfunction is applied after data is loaded. - It skips rendering of inactive rows.
β HTML Page:
<div
appml-controller="filter-controller.js"
appml-model="models/tasks-model.json"
appml-data="tasks.php">
<h2>Active Tasks</h2>
<p>{{title}} β Status: {{status}}</p>
</div>
βοΈ The view only displays tasks marked as βactive,β thanks to the logic defined in the controller.
π AppML Controller Lifecycle Summary
Hereβs a simplified flow of how controllers interact with AppML:
Page loads
β
appml-controller loads
β
onready() β Initialization
β
Data fetched via appml-data
β
onshow() β Filtering, formatting
β
User interaction (submit/update)
β
onvalidate() β Field validation
β
oninsert()/onupdate() β Before save
β
onsave() β After record saved
π Summary β Recap & Key Takeaways
AppML controllers allow you to inject intelligence into your HTML apps. Whether itβs validating input, filtering views, or triggering logic, controllers give you powerful hooks without needing a large JavaScript codebase.
π Key Takeaways:
- Controllers are simple JS files attached with
appml-controller - Use lifecycle methods like
onready,onshow,onvalidate, etc. myAppML.datagives access to the current record being processed- Return
falseinonvalidateto prevent bad submissions - Ideal for low-code devs needing custom logic without full JS frameworks
βοΈ AppML controllers are the lightweight solution for adding logic to low-code HTML applications.
β FAQs β AppML Controllers Explained
β Do I need a separate controller for each view?
β
Yes, each AppML view can load a different controller script for modular logic.
β What happens if I forget to return true in onvalidate()?
β If you return nothing or omit true, the form may not submit. Always explicitly return true or false.
β Can I log values in the controller for debugging?
β
Yes. Use console.log(myAppML.data) to inspect the data being handled.
β Can I modify data inside a controller before saving?
β
Yes, use oninsert() or onupdate() to change field values programmatically.
β Are controllers required in every AppML app?
β No. Use controllers only if you need to extend AppMLβs default behavior.
Share Now :
