πŸ“‘ AppML Controllers & Models
Estimated reading: 4 minutes 42 views

🧱 AppML – Defining and Using Models: Structure Your Data the Right Way

🧲 Introduction – Why AppML Models Matter

In AppML, models are the blueprint that define how data should behave in your application. Unlike raw JSON or XML files, a model adds structure, validation rules, and dynamic control to your AppML-based apps.

🎯 In this article, you’ll learn:

  • What AppML models are and why you should use them
  • How to define models using .json or .xml
  • How models work with controllers and views
  • Real examples for input validation, schema constraints, and CRUD operations

🧩 What Is an AppML Model?

An AppML model is a structured definition (usually a .json file) that describes the fields, data types, rules, and validation for a dataset.

It helps AppML understand how to:

  • Render input forms
  • Perform client-side and server-side validation
  • Filter, sort, and manage data
  • Connect views and controllers to the correct schema

πŸ—‚οΈ Sample AppML Model Structure (JSON)

βœ… models/product-model.json

{
  "tablename": "products",
  "orderby": "name",
  "fields": [
    { "name": "id", "primarykey": true },
    { "name": "name", "required": true },
    { "name": "price", "datatype": "number", "required": true },
    { "name": "category" }
  ]
}

🧾 Breakdown:

πŸ”‘ Property🧠 Meaning
tablenameName of the database table
orderbyDefault sort field
fieldsList of columns with rules
primarykeyUnique identifier for each record
requiredField must not be empty
datatypeOptional: string, number, date, etc.

πŸ§ͺ Example – Using a Model in HTML View

<div appml-model="models/product-model.json" appml-data="get-products.php">
  <input name="name">
  <input name="price" type="number">
  <input name="category">
  <button appml-submit>Save</button>
</div>

βœ… The model guides how fields are displayed and validated before submission.


🧰 Advanced Use – With Controller Logic

Combine models with a controller for intelligent behavior:

myAppML.onvalidate = function() {
  if (myAppML.data.price < 1) {
    alert("Price must be at least $1");
    return false;
  }
  return true;
};

🎯 This enforces dynamic validation beyond the static rules in the model.


πŸ’» Using AppML Models with Databases

The tablename field in the model links it to a backend SQL table (via PHP or ASP).

βœ… Backend: get-products.php

$mysqli = new mysqli("localhost", "root", "", "shop");
$sql = "SELECT * FROM products ORDER BY name";
$result = $mysqli->query($sql);

$data = [];
while ($row = $result->fetch_assoc()) {
  $data[] = $row;
}
echo json_encode($data);

With appml-model="product-model.json", AppML will auto-match the data fields to the model schema.


πŸ› οΈ XML-Based Model Alternative

Although JSON is preferred, AppML supports XML too:

βœ… product-model.xml

<model>
  <tablename>products</tablename>
  <orderby>name</orderby>
  <fields>
    <field name="id" primarykey="true" />
    <field name="name" required="true" />
    <field name="price" datatype="number" required="true" />
    <field name="category" />
  </fields>
</model>

Use it like:

<div appml-model="models/product-model.xml" appml-data="get-products.php">
  ...
</div>

πŸ“Œ Summary – Recap & Key Takeaways

Models define the structure and rules of your application’s data in AppML. By declaring fields, data types, and validation logic, they give your apps clarity and reusability.

πŸ” Key Takeaways:

  • AppML models define the data schema (field names, types, requirements)
  • Used via appml-model="..." attribute
  • Supports JSON (preferred) and XML
  • Integrates with controllers for advanced behavior
  • Great for forms, input validation, and CRUD

βš™οΈ Models separate logic from presentation, making your apps scalable and easier to maintain.


❓ FAQs – AppML Models


❓ Is the model file required in every AppML app?
❌ No, but it is highly recommended for structured data and form input scenarios.


❓ Can I use both appml-model and appml-controller together?
βœ… Yes, and this is a best practice for apps that need logic + schema validation.


❓ What happens if I leave out datatype in a field?
βœ… It defaults to “string”. You can leave it out for simple text fields.


❓ Can I update the model at runtime?
⚠️ Not directly. You’ll need to reload or swap the appml-model source file.


❓ Can I use models with both static JSON and server-side PHP?
βœ… Absolutely. The model defines structure; data can come from any source.


Share Now :

Leave a Reply

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

Share

AppML – Defining and Using Models

Or Copy Link

CONTENTS
Scroll to Top