π§± 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 |
---|---|
tablename | Name of the database table |
orderby | Default sort field |
fields | List of columns with rules |
primarykey | Unique identifier for each record |
required | Field must not be empty |
datatype | Optional: 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 :