ข้อความAppML
ข้อความและการดำเนินการของ AppML
เมื่อ AppML กำลังจะดำเนินการ มันจะส่งวัตถุของแอปพลิเคชัน ($appml) ไปยังตัวควบคุม
หนึ่งในคุณสมบัติของวัตถุแอปพลิเคชันคือข้อความ ($appml.message) ที่อธิบายสถานะของแอปพลิเคชัน
การทดสอบข้อความนี้ทำให้คุณสามารถเพิ่มโค้ด JavaScript ของคุณเองได้ ขึ้นอยู่กับการดำเนินการ
ตัวอย่าง
function myController($appml) {
if ($appml.message == "ready") {alert ("Hello
Application");}
}
ข้อความ AppML
นี่คือรายการข้อความ AppML ที่สามารถรับได้:
Message | Description |
---|---|
"ready" | Sent after AppML is initiated, and ready to load data. |
"loaded" | Sent after AppML is fully loaded, ready to display data. |
"display" | Sent before AppML displays a data item. |
"done" | Sent after AppML is done (finished displaying). |
"submit" | Sent before AppML submits data. |
"error" | Sent after AppML has encountered an error. |
ข้อความ "พร้อม"
เมื่อแอปพลิเคชัน AppML พร้อมที่จะโหลดข้อมูล แอปพลิเคชันจะส่งข้อความ "พร้อม"
นี่เป็นสถานที่ที่สมบูรณ์แบบในการให้ข้อมูลเริ่มต้นแก่แอปพลิเคชัน (ค่าเริ่มต้น):
ตัวอย่าง
<div appml-controller="myController" appml-data="customers.js">
<h1>Customers</h1>
<p>{{today}}</p>
<table>
<tr>
<th>Customer</th>
<th>City</th>
<th>Country</th>
</tr>
<tr appml-repeat="records">
<td>{{CustomerName}}</td>
<td>{{City}}</td>
<td>{{Country}}</td>
</tr>
</table>
<p>Copyright {{copyright}}</p>
</div>
<script>
function myController($appml) {
if ($appml.message == "ready") {
$appml.today = new Date();
$appml.copyright = "W3Schools"
}
}
</script>
ในตัวอย่างข้างต้น เมื่อ$appml.messageเป็น "พร้อม" ผู้ควบคุมจะเพิ่มคุณสมบัติใหม่สองรายการให้กับแอปพลิเคชัน ( วันนี้และลิขสิทธิ์ )
เมื่อแอปพลิเคชันทำงาน คุณสมบัติใหม่จะพร้อมใช้งานสำหรับแอปพลิเคชัน
ข้อความ "โหลด"
เมื่อแอปพลิเคชัน AppML โหลดข้อมูล (พร้อมที่จะแสดง) แอปพลิเคชันจะส่งข้อความ " โหลดแล้ว"
นี่คือตำแหน่งที่สมบูรณ์แบบในการเปลี่ยนแปลง (หากจำเป็น) กับข้อมูลที่โหลด
ตัวอย่าง
function myController($appml) {
if ($appml.message == "loaded") {
// compute your values here before display
}
}
ข้อความ "แสดง"
ทุกครั้งที่ AppML แสดงรายการข้อมูล ระบบจะส่งข้อความ " แสดง "
นี่คือตำแหน่งที่สมบูรณ์แบบในการแก้ไขผลลัพธ์:
ตัวอย่าง
<div appml_app="myController" appml-data="customers.js">
<h1>Customers</h1>
<table>
<tr>
<th>Customer</th>
<th>City</th>
<th>Country</th>
</tr>
<tr appml-repeat="records">
<td>{{CustomerName}}</td>
<td>{{City}}</td>
<td>{{Country}}</td>
</tr>
</table>
</div>
<script>
function myController($appml) {
if
($appml.message == "display") {
if ($appml.display.name ==
"CustomerName") {
$appml.display.value = $appml.display.value.substr(0,15);
}
if ($appml.display.name == "Country") {
$appml.display.value = $appml.display.value.toUpperCase();
}
}
}
</script>
ในตัวอย่างข้างต้น "CustomerName" จะถูกตัดให้เหลือ 15 อักขระ และ "Country" จะถูกแปลงเป็นตัวพิมพ์ใหญ่
ข้อความ "เสร็จแล้ว"
เมื่อแอปพลิเคชัน AppML แสดงข้อมูลเสร็จแล้ว แอปพลิเคชันจะส่งข้อความ " เสร็จสิ้น "
นี่คือที่ที่เหมาะสำหรับการล้างหรือคำนวณข้อมูลแอปพลิเคชัน (หลังจากแสดงผล)
ตัวอย่าง
<script>
function myController($appml) {
if ($appml.message == "done") {
calculate data here
}
}
</script>
ข้อความ "ส่ง"
เมื่อแอปพลิเคชัน AppML พร้อมที่จะส่งข้อมูล แอปพลิเคชันจะส่ง ข้อความ " ส่ง "
นี่เป็นสถานที่ที่สมบูรณ์แบบในการตรวจสอบการป้อนข้อมูลของแอปพลิเคชัน
ตัวอย่าง
<script>
function myController($appml) {
if ($appml.message == "submit") {
validate data here
}
}
</script>
ข้อความ "ผิดพลาด"
หากเกิดข้อผิดพลาด AppML จะส่งข้อความ " ข้อผิดพลาด "
นี่คือสถานที่ที่สมบูรณ์แบบในการจัดการข้อผิดพลาด
ตัวอย่าง
<script>
function myController($appml) {
if ($appml.message ==
"error") {
alert ($appml.error.number + " " + $appml.error.description)
}
}
</script>
คุณสมบัติ AppML
นี่คือรายการคุณสมบัติ AppML ที่ใช้กันทั่วไปบางส่วน:
Property | Description |
---|---|
$appml.message | The current state of the application. |
$appml.display.name | The name of the data field about to be displayed. |
$appml.display.value | The value of the data field about to be displayed. |
$appml.error.number | The error number. |
$appml.error.description | The error description. |