πŸ’‘ AppML Advanced Topics
Estimated reading: 4 minutes 28 views

βš™οΈ 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, and onvalidate
  • 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 FunctionTriggered 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:

LinePurpose
onreadyFires once the AppML view is initialized and DOM is loaded
onshowExecutes 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:

CodeWhat It Does
onvalidateRuns when the user clicks the submit button
myAppML.data.email.includes("@")Checks if the email input is valid
return falseCancels form submission if the condition fails
myAppML.messageDisplays 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() and onshow() for UI logic after loading
  • Use onvalidate() to intercept form submissions
  • Use oninsert() and onupdate() 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

AppML – Event Handling in AppML

Or Copy Link

CONTENTS
Scroll to Top