βοΈ AppML β Event Handling in AppML: Add Interactivity Without JavaScript Frameworks
π§² Introduction β Why Handle Events in AppML?
Even in low-code environments like AppML, applications need to respond to user actionsβlike clicking a button, submitting a form, or loading data. AppML supports event hooks via a controller file, allowing you to execute logic at different stages of the application lifecycleβwithout needing a full JavaScript framework.
π― In this tutorial, you’ll learn:
- What events are available in AppML
- How to use AppMLβs event methods like
onready
,onshow
, andonvalidate
- How to attach a controller to handle those events
- Real-world examples with code and detailed explanations
π AppML Event Lifecycle Overview
AppML controllers let you hook into lifecycle events using these predefined functions:
Event Function | Triggered When⦠|
---|---|
onready() | AppML app is initialized and ready |
onshow() | Data has been loaded and rendered to HTML |
onvalidate() | Form is submittedβused for client-side validation |
oninsert() | A new record is about to be inserted |
onupdate() | A record is about to be updated |
onsave() | After a record is inserted/updated |
All of these are defined on the global myAppML
object inside a controller JavaScript file.
π§ͺ Example 1 β Setup onready
and onshow
β
controller.js
myAppML.onready = function() {
console.log("AppML is initialized.");
};
myAppML.onshow = function() {
alert("Data has been loaded successfully!");
};
β Explanation:
Line | Purpose |
---|---|
onready | Fires once the AppML view is initialized and DOM is loaded |
onshow | Executes after data from appml-data is fetched and rendered |
β HTML Page:
<div
appml-controller="controller.js"
appml-model="models/users-model.json"
appml-data="users.php">
<h2>User List</h2>
<p>{{name}} β {{email}}</p>
</div>
βοΈ When the view is loaded, onready
logs to the console and onshow
displays an alert once data is rendered.
π§ͺ Example 2 β Using onvalidate()
to Prevent Invalid Submission
β
Controller File: form-controller.js
myAppML.onvalidate = function() {
if (!myAppML.data.email.includes("@")) {
myAppML.message = "Please enter a valid email address.";
return false;
}
return true;
};
β Explanation:
Code | What It Does |
---|---|
onvalidate | Runs when the user clicks the submit button |
myAppML.data.email.includes("@") | Checks if the email input is valid |
return false | Cancels form submission if the condition fails |
myAppML.message | Displays an error message automatically in the view |
β HTML Form:
<div
appml-controller="form-controller.js"
appml-model="models/form-model.json"
appml-data="form-handler.php"
appml-message>
<input name="email" placeholder="Your Email">
<button appml-submit>Submit</button>
</div>
βοΈ This form wonβt submit unless the entered email contains β@β.
π§ͺ Example 3 β Track Insert and Update Events
β
crud-controller.js
myAppML.oninsert = function() {
console.log("New record is being inserted.");
};
myAppML.onupdate = function() {
console.log("Record is being updated.");
};
myAppML.onsave = function() {
alert("Save operation completed successfully.");
};
β Explanation:
oninsert
: Runs before inserting a new entry.onupdate
: Fires before updating an existing record.onsave
: Executes after either insert or update succeeds.
π These can be used for:
- Data transformation
- Logging
- User notifications
- Conditional redirects
π Complete Event Flow in AppML
Page Loads
β
appml-controller is loaded
β
onready() β initialize logic or prefetch settings
β
appml-data fetches data
β
onshow() β filter/format results
β
User edits or submits form
β
onvalidate() β optional field checks
β
oninsert() or onupdate()
β
onsave() β final post-save action
π Summary β Recap & Key Takeaways
AppML provides event handling through simple JavaScript controller files. You can run logic at every stageβapp initialization, data loading, form validation, insert/update, and save confirmation.
π Key Takeaways:
- Use
onready()
andonshow()
for UI logic after loading - Use
onvalidate()
to intercept form submissions - Use
oninsert()
andonupdate()
to manage records - Use
onsave()
for final notifications or page transitions
βοΈ With AppMLβs event handling, you can keep your HTML smart and reactiveβwithout needing React or Vue.
β FAQs β Event Handling in AppML
β Do I need JavaScript frameworks to use AppML events?
β
No. AppML handles events with plain JavaScript in controller files.
β Can I use more than one event in a single controller?
β
Yes. You can define multiple lifecycle functions like onready
, onvalidate
, onshow
, etc., in the same file.
β What if I forget to return true
in onvalidate()
?
β The form may not submit. Always explicitly return true
to allow submission.
β Can I use event handlers with lists and forms both?
β
Yes. Events apply to any AppML viewβlists, tables, forms, dashboards, etc.
β Can I conditionally skip saving a record in oninsert()
?
β
Yes. You can return false
in oninsert()
or onupdate()
to cancel the action.
Share Now :