Wednesday, October 1, 2008

for … in Statement

ဒီ for.. in ကို javascript array ရဲ ့ element တစ္ခုခ်င္းစီကို ပတ္ခ်င္တဲ့ အခါမွာ သံုးပါတယ္။

ဥပမာ

<html>
<body>
<script type="text/javascript">

var x;
var mygreeting= new Array();

mygreeting[0] = "Hello";
mygreeting[1] = "World";
mygreeting[2] = "Greeting by Akela.";

for (x in mygreeting)
{
alert(mygreeting[x]);
}

</script>
</body>
</html>

အထက္ပါ ကုဒ္ေလးကို ခ်ခိုင္းတဲ့ အခါ ေအာက္ပါပံုအတိုင္း ၿမင္ရပါလိမ့္မယ္။




အာေကလာ

၂၇ စက္တင္ဘာ ၂၀၀၇

Thursday, September 18, 2008

Array တစ္ခု တည္ေဆာက္ၿခင္း

Javascript Array Object

Array တစ္ခုဆိုတာ variable တစ္ခုၿဖစ္ၿပီး တန္ဖိုးေတြ အမ်ားၾကီး သိမ္းဆည္းထားလို ့ ရပါတယ္။ javascript ရဲ ့ array ဟာ တၿခား programming ဘာသာရပ္ေတြ က array ေတြ လိုပါပဲ။
ေအာက္ပါအခ်က္မ်ားကို javascript မွာ array ကိုသံုးတဲ့ အခါ သိမွတ္ထားသင့္ပါတယ္။

၁။ ။ တန္းဖိုးေတြဟာ array တစ္ခုထဲကို သိမ္းဆည္းေတာ့မယ္ဆိုရင္ array နာမည္နဲ ့ ေနရာကို ေဖာ္ၿပၿပီး သိမ္းဆည္းႏုိင္ပါတယ္။ ဥပမာ - myFirstArray[2] = “Hello JS Array”;


၂။ ။ သိမ္းထားတဲ့ တန္းဖိုးေတြကို ၿပန္ၿပီးထုတ္ယူ သံုးစြဲၿခင္တဲ့ အခါမွာလည္း array နာမည္နဲ ့ ေနရာကို ေဖာ္ၿပၿပီး ၿပန္ထုတ္ႏိုင္ပါတယ္။ ဥပမာ - var varlue = myFirstArray[2]; (ဒါဆို ခုနက Hello JS Array ဆိုတဲ့ တန္ဖိုးကို ၿပန္ရပါမယ္။)

ေအာက္ပါ နည္းလမ္းနဲ ့ Array တစ္ခုကို တည္ေဆာက္လုိ ့ရပါတယ္။


 Var myFirstArray = new Array(3);

ဒါဆိုရင္ ကၽြန္ေတာ္တို႔ရဲ႕ myFirstArray ရဲ႕ length property ဟာ ၃ ျဖစ္သြားပါပီ။
myFirstArray ကို value ေတြထပ္ျဖည့္ရေအာင္.....

myFirstArray[0] = "This is first Item"; // Javascript ရဲ႕ Array က index 0 ကေနစတာပါ
myFirstArray[1] = new Object();
myFirstArray[2] = new Date();


ေနာက္တစ္မ်ိဳးစမ္းၾကည့္ရေအာင္

var myFirstArray = new Array();
myFirstArray.push("This is first Item");
myFirstArray.push(new Object());
myFirstArray.push(new Date());


Array ရဲ႕ push method ကိုသံုးရင္ Array object က Array ရဲ႕ length ကို auto increment လုပ္ေပးသြားပါတယ္။
အခုအခ်ိန္ myFirstArray.pop() ဆိုပီး Array object ရဲ႕ pop method ကိုေခၚလုိက္ရင္ myFirstArray ရဲ႕ length က ၁ ေလ်ာ့သြားပါပီ။
အဲဒီလိုပဲ myFirstArray ရဲ႕ အႀကီးဆံုး index က value (ဥပမာ ထဲက အတိုင္းဆိုရင္ new Date()) ကပ်က္သြားပါတယ္။
var lastVar = myFirstArray.pop() ဆိုရင္ myFirstArray ရဲ႕ ေနာက္ဆံုး index က value က lastVar ဆိုတဲ့ variable ထဲကိုေရာက္သြားပါမယ္။
myFirstArray ထဲကလည္း ပ်က္သြားပါပီ။ myFirstArray ရဲ႕ length property ကလည္း 2 ျဖစ္သြားပါမယ္။
(myFirstArray.length)



အာေကလာ၊ အဇူရီ
၁၄ စက္တင္ဘာ ၂၀၀၈

Fundamental concept of WCF (Microsoft Window Communication Foundation )

(ဒီပိုစ္ကို ၁၈ စက္တင္ဘာ ၂၀၀၇ အမွတ္တရ ဂုဏ္ၿပဳ ေရးပါသည္။)

အခုေနာက္ပုိင္း business solution အတြက္ software ေတြ ကိုေရးတဲ့အခါမွာ stand alone application ေတြ ေတာ္ေတာ္ရွားပါးသြားပါၿပီ။ distributed application ေတြပဲ ေရးလာၾကပါတယ္။ Microsoft ကလည္း .net framework 2.0 နဲ ့ ေနာက္ပိုင္းေတြမွာ ဒီအတြက္ အထူးတလည္ ၿပင္ဆင္ေပးခဲ့ပါတယ္။ စစခ်င္း .net remotnig ေနာက္ပိုင္းေတာ့ Window Communication Foundation (WCF) ဆိုၿပီးလုပ္လာပါတယ္။ အခုကၽြန္ေတာ္ ေရးမွာက WCF ရဲ ့အေၿခခံပါပဲ။ Fundamental Concepts ေတြပါ။ High-level View ပါပဲ။ ဒါေပမယ့္ ဒါေတြ သိထားမွ ဘယ္လို အလုပ္လုပ္လည္း ဆိုတာသိႏိုင္မွာၿဖစ္တယ္။ ဆက္ေလ့လာလို ့လြယ္ကူေစမွာၿဖစ္တဲ့ အတြက္ေၾကာင့္ပါ။ ၾကည့္ၾကစို ့ရဲ ့။

Windows Communication Foundation (WCF)

(၁) High-level Overview
(၁.၁) Messaging and Endpoints


WCF ဆိုတာ runtime တစ္ခု၊ ေနာက္ၿပီး services နဲ ့ clients ေတြၾကား messages ေတြ ပို ့တဲ့ စနစ္ (system) ေတြဖန္တီးဖို ့့ APIs ေတြစုစည္းထားတဲ့ ေနရာတစ္ခုပါ။ အဲဒါကို သံုးၿပီးေတာ့ တစ္ခုနဲ ့တစ္ခု ဆက္သြယ္လို ့ရတဲ့ application ေတြကို တည္ေဆာက္ယူလို ့ရပါတယ္။ အဲဒီ applications ေတြဟာ ကြန္ၿပဳတာတစ္လံုးထဲမွာလည္း ၿဖစ္ခ်င္ၿဖစ္မယ္၊ ဒါမွမဟုတ္ တၿခားတေနရာ၊ တၿခားႏိုင္ငံ၊ တၿခားကုမဏီတစ္ခု က ကြန္ၿပဳတာတစ္လံုးေပၚ မွာလည္းၿဖစ္ခ်င္ၿဖစ္မယ္။ ဒါေပမယ့္ WCF က services ေတြ၊ APIs ေတြ သံုးၿပီး ဆက္သြယ္လုပ္ရတယ္။ အတူတူ လုပ္ေဆာင္လို ့ရတယ္။

WCF ဟာ Messaging ေပၚမွာ အေၿခခံထားတဲ့ ေမာ္ဒယ္တစ္ခုပါပဲ။ အဲဒီထဲမွာ clients လို ့ေခၚတဲ့ ဆက္သြယ္မွဳ ့ (communication) ကို စမဲ့ application ေတြ၊ ေနာက္ services လို ့ေခၚတဲ့ clients ေတြ ဆက္သြယ္လာမွာကို ေစာင့္ဆိုင္းေနၿပီး တုန္ၿပန္ေပးမဲ့ application ေတြပါပါတယ္။ သတိၿပဳရမွာက application တစ္ခုတည္းက လည္း client ေရာ service ပါၿဖစ္ႏိုင္တယ္ဆိုတာပါပဲ။

Message ေတြဟာ endpoints ေတြၾကားမွာ ေပးပို ့ခံရပါတယ္။ endpoints ဆိုတာ message ေတြပို ့တဲ့ ေနရာ (send) ဒါမွမဟုတ္ လက္ခံတဲ့ေနရာ (receive) ပါ။ အဲဒီ endpoints ေတြကပဲ message ပို ့တဲ့၊ လက္ခံတဲ့ အခါမွာ လိုအပ္မဲ့ Information ေတြအားလံုးကို သတ္မွတ္ၿပဒါန္းေပးပါတယ္။ service တခုက တစ္ခု ဒါမွမဟုတ္ တစ္ခုထက္ပိုတဲ့ endpoints ေတြကို တည္ေဆာက္ေေပးထားတက္ပါတယ္။ client ေတြကေတာ့ services ေတြတည္ေဆာက္ေပးထားတဲ့ endpoint တစ္ခုခုနဲ့ ကိုက္ညီတဲ့ endpoint တစ္ခုကို တည္ေဆာက္ရပါတယ္။

Service တစ္ခုဟာ message ေတြကို ဘယ္လို ပို ့ရတယ္၊ ဘယ္လို ပံုစံရိွရမယ္၊ ဘယ္ကို ပို ့ရမယ္ဆိုတာေတြ၊ အၿခားလိုအပ္တဲ့ အရာေတြ အားလံုးကို metadata (data about data) အေနနဲ ့ေဖၚၿပထားတယ္။ အဲဒီအခါ client က အဲဒီ metadata ေတြ သံုးၿပီး သင့္ေတာ္တဲ့ WCF client နဲ ့ Communication stack ေတြကို တည္ေဆာက္ပါတယ္။

(WCF client, Communication stack ေတြ ဘာလဲဆိုတာ နားမလည္ရင္လည္းဆက္ဖတ္ပါ ေအာက္မွာ အကုန္ၿပန္ပါပါတယ္။)

(၁.၂) Communication Protocols

WCF ရဲ ့အဓိကအခ်က္က ေနရာမေရြး (ေနရာမ်ိဳးစံုက client ေတြက) service ေတြကို ဆက္သြယ္ႏိုင္တာဆိုေတာ့၊ network လိုလာပါတယ္။ အဲဒီ network ေပၚမွာ သံုးမဲ့ protocols ေတြဟာလည္း လိုအပ္လာပါတယ္။ ဒါေၾကာင့္
Communication stacks ေတြရဲ ့ လိုအပ္ခ်က္ တစ္ခုကေတာ့ transport protocol ေတြပါပဲ။ Messages ေတြကို ကၽြန္ေတာ္တို ့က intranets, Internet ေပၚကေန HTTP, TCP လို common transport protocol ေတြ သံုးၿပီး ေေနရာမ်ိဳးစံုကေန ေပးပို ့ ဆက္သြယ္ႏိုင္ပါတယ္။ ေနာက္ထပ္ transport ေတြကေတာ့ Microsoft Message Queuing (MSMQ) တုိ ့၊ Peer Networking mesh တို ့ပါပဲ။ တၿခား transport mechanisms ေတြကိုလည္း WCF ရဲ ့ ပါၿပီးသား (build-in) extension points ေတြသံုးၿပီး ထပ္ထည့္ႏိုင္ပါတယ္။

(၁.၃) Message Patterns

WCF မွာ အမ်ိုးမ်ိဳးေသာ message patterns ေတြကို သံုးႏိုင္ပါတယ္။ request-reply၊ one-way၊ duplex communication စသည္ၿဖင့္အသံုးၿပဳႏိုင္ပါတယ္။ ကြဲၿပားတဲ့ transports ေတြက ကြဲၿပားတဲ့ message patterns ေတြကို သံုးပါတယ္။ WCF APIs နဲ ့ runtime ေတြက message ေတြရဲ ့လံုၿခံဳမွဳကိုလည္းလုပ္ေပးပါတယ္။

(၂) WCF Terms

(၁) message
Message ဆိုတာ data ေတြပါ၀င္ဖြဲ ့စည္းထားတဲ့ အစုတခု။ သူမွာ body ၊ header စတာေတြအပါအ၀င္ တၿခားအစိတ္အပိုင္း ေတြလည္းပါ၀င္တယ္။

(၂) service
Service ဆိုတာကေတာ့ endpoint တစ္ခု ဒါမွမဟုတ္ တစ္ခုထက္ပိုၿပီး တည္ေဆာက္ေပး ထားတဲ့ ေနရာပါပဲ။ အဲဒီ endpoint တစ္ခုစီဟာ service operation တခု ဒါမွမဟုတ္ တစ္ခုထက္ပို ၿပီးရိွတက္တယ္။

(၃) endpoint
Endpoint ဆိုတာ message ေတြ ပို ့ရမဲ့၊ လက္ခံတဲ့ ေနရာတခုပါပဲ။ တခ်ိဳ ့ endpoint ေတြ
ကလက္ခံတဲ့ ဒါမွမဟုတ္ ပို ့တဲ့ အလုပ္တမ်ိဳးကိုပဲလုပ္ၿပီး ၊တခ်ိုဳ ့ကေတာ့ ႏွစ္မ်ိဳး လံုးကိုလုပ္ပါတယ္။ endpoint ေတြူက message ေတြ ပို ့ဖို ့အတြက္ လိပ္စာ (address) တစ္ခု ထုတ္ေပးတားတယ္။ ေနာက္ ဘယ္လိုနည္းနဲ ့ဆက္သြယ္လို ့ရတယ္ (communication mechanism) ဆိုတဲ့ အေၾကာင္းအရာ သတ္မွတ္ခ်က္ေတြ (specification) ကိုလည္း ထုပ္ေပးထားတယ္္။ ေနာက္ဆံုး ဘယ္လို message ေတြပို ့လို ့ ရတယ္ဆိုတာေတြပါ ေဖာ္ၿပေပးထားပါတယ္။ WCF service တစ္ခုဆိုတာဟာ endpoint ေတြ စုစည္းထားတဲ့ ေနရာတစ္ခုပါပဲ။ (is a world as a collection of service.)

(၄) address
Address တခုဟာ message ေတြကို ဘယ္မွာလက္ခံမယ္ဆိုတဲ့ ေနရာတခုကို ေၿပာၿပေပးပါတယ္။ သူကို Uniform Resource Identifier (URI) အေနနဲ ့သတ္မွတ္ေလ့ရိွတယ္။ ဒါေၾကာင့္ endpoint address ေတြဟာ ထပ္တူမရိွတဲ့ address (Unique Address) ေတြပါ။

(၅) binding
endpoint တခုဟာ ၿပင္ပ နဲ ့ဘယ္လို ဆက္သြယ္လုပ္ေဆာင္မလဲဆိုတာကို binding ကသတ္မွတ္ေပးပါတယ္။ Communication Infrastructure ၿဖစ္လာဖို ့အတြက္ binding element ေတြလို ့ေခၚဆိုတဲ့ components အစုေလးေတြ တခုေပၚတခု (stack on top of the other) တည္ေဆာက္ထားတယ္။ အႏိွမ့္ဆံုးအေနနဲ ့ binding က ဘယ္လို transports ေတြသံုးမယ္( HTTP , TCP/IP) ၊ ဘယ္လုိ encoding ေတြသံုးမယ္ (text or binary) စတာေတြကို သတ္မွတ္ေပးပါတယ္။ security နဲ ့ဆိုင္တဲ့ ကိစၥေတြ၊ endpoint ကသံုးမယ့္ message pattern ေတြကိုလည္း သတ္မွတ္ေပးပါတယ္။

(၆) binding element
Binding element တခုဟာ binding လုပ္ခ်င္းကိစၥရဲ ့အစိတ္အပိုင္းတခုကို ကိုယ္စားၿပဳပါတယ္။ transport တခု၊ encoding တခု စသည္အားၿဖင့္။ ဒါမွမဟုတ္ communication stack ေပၚမွာ ရိွတဲ့ ဘယ္ component မဆို binding element ေတြပါပဲ။

(၇) behaviors
Behavior ဆိုတာ client တခု၊ operation တခု၊ ဒါမွမဟုတ္ endpoint တခု၊ service တခု ကို run-time မွာ ဘယ္လို လုပ္သလဲဆိုတာကို ထိန္းတဲ့ component တခုပါပဲ။ behavior ေတြကို သူတို ့ရဲ ့ scope ေပၚမူတည္ၿပီး အဖြဲ ့ေလးေတြ ဖြဲ ့ထားပါတယ္။ common behaviors လို ့ဆိုရင္ endpoint အားလံုးနဲ ့ဆိုင္ပါတယ္။ globally ေပါ့။ service behavior ဆိုရင္ service နဲ ့ ဆက္ႏြယ္ေနတဲ့ကိစၥ ေတြနဲ ့ဆိုင္ပါတယ္။ endpoint behavior လို ့ဆိုရင္ endpoint နဲ ့ဆက္ႏြယ္ေနတဲ့ အသံုးၿပဳမွဳ ေတြနဲ ့ ဆိုင္ပါတယ္။ operation-level behaviors ဆိုရင္ေတာ့ operation တစ္ခုခ်င္းစီနဲ ့ သက္ဆိုင္ပါတယ္။ ဥပမာ- endpoint behaviors ဆိုပါစို ့။ သူက endpoint နဲ ့ပဲဆိုင္တဲ့ အရာေတြကို ၿပဥာန္းေပးပါတယ္။ security credential ကိုဘယ္မွ သြားရွာရမလဲဆိုတာမ်ိဳးေပါ့။

(၈) configuration နဲ ့ coding
Application တခုကို ထိန္းခ်ဳပ္တဲ့ ကိစၥမွာ coding ကေနတဆင့္ ၊ ဒါမွမဟုတ္ configuration file ကေနတဆင့္ ၊ (၀ါ) ႏွစ္ခုလံုး ကို သံုးၿပီး လုပ္ႏိုင္ပါတယ္။ configuration ကေန လုပ္တာက programmer (developer) မဟုတ္တဲ့ သူေတြကိုပါ client (၀ါ) service မွာ လိုတဲ့ အရာေတြကို coding ေရးၿပီးေပ့မယ့္ ၿပဳၿပင္ခြင့္၊ ထိန္းခ်ဳပ္ခြင့့္ ေပးတဲ့ အားသာခ်က္ရိွပါတယ္။ အဲဒီလုိလုပ္တဲ့ ကိစၥမွာ program coding ကို ၿပန္ၿပီး recompile လုပ္စရာမလိုပဲ သံုးလို ့ရပါတယ္။ coding ကေတာ့ programmer (developer) ေတြကို client (၀ါ) service ေတြ အေပၚမွာ တိက်တင္းက်ပ္တဲ့ ထိန္းခ်ဳပ္မွဳေတြ လုပ္ဖို ့ ခြင့္ၿပဳပါတယ္။ configuration ဖိုင္ကေန စီစဥ္ထိန္းခ်ဳပ္ထားတဲ့ ဘယ္အရာကိုမဆို လိုအပ္တယ္ဆိုရင္ coding ကေန ေၿပာင္းလဲ ႏိုင္ပါတယ္။

(၉) service operation
Service operation တခုက service တခု လုပ္ေဆာင္မဲ့ ကိစၥတခု (operation) ကို service code ေတြကတဆင့္ သတ္မွတ္ထားတဲ့ ဟာကို ေၿပာတာပါ။ ဒီ service တခုကလုပ္ေဆာင္ေပးမဲ့ အရာေတြကို WCF Client ေတြဆီ method ေတြအေနနဲ ့ ထုတ္ေပးထားတယ္။ အဲဒီ method ေတြက တန္ဖိုး တခုကို ၿပန္ရင္ ၿပန္မယ္။ သူဆီကို arguments ေတြ လက္ခံရင္ လက္ခံမယ္။ ဒါမွမဟုတ္ ဘာတန္ဖိုးမွ မၿပန္တာ၊ ဘာ argument မွ လက္မခံတာ။ ဒီလိုလည္းၿဖစ္ႏိုင္ပါတယ္။

(၁၀) service contract
Service contract ဆိုတာကေတာ့ ခုနက service operation ေတြကို စုစည္းေပးထားတဲ့ အစုတစ္ခုပါပဲ။ Contract တခုကို အမ်ိဳးမ်ိဳးေသာ အဆင့္ေတြ သတ္မွတ္လို ့ရတယ္။ ဥပမာ- namespace အဆင့္မွာ သတ္မွတ္တဲ့ contract။ အမ်ားဆံုးလုပ္ေလ့ရိွတာကေတာ့ ကိုယ္ၾကိဳက္တဲ့ program ဘာသာရပ္ (C#, VB, J#) နဲ ့ interface တခုေဆာက္ၿပီး အဲဒီမွာ ServiceContractAttribute ဆိုတဲ့ attribute ကို ေၾကညာေပးလိုက္တာပဲ။ တကယ္ အလုပ္လုပ္မဲ့ service code ေတြက အဲဒီ interface ကို အေမြဆက္ခံ (inherit) လိုက္တာပါပဲ။

(၁၁) operation contract
operation contract ဆိုတာကေတာ့ operation တစ္ခုဆီကို ပို ့ေပးရမယ့္ parameters ေတြ၊ ဒါမွမဟုတ္ အဲဒီ operation ကေန ၿပန္မဲ့ အမ်ိဳးအစားေတြ ကို သတ္မွတ္ေပးပါတယ္။ ကၽြန္ေတာ္တို ့ service contract နဲ ့ တည္ေဆာက္ထားတဲ့ (အေပၚကအဆင့္) interface တစ္ခုမွာ operation contract ကို OperationContractAttribute ဆိုတဲ့ attribute ေလးကို method ရဲ ့ အေပၚမွာ ထည့္ေပးလိုက္ရံုနဲ ့ တည္ေဆာက္ႏိုင္ပါတယ္။ အဲဒါဆို အဲဒီ method ေလး (WCF အေနနဲ ့ဆို operation ေလးဟာ) ဟာ operation contract နဲ ့ ၿဖစ္သြားပါၿပီ။

(၁၂) message contract
message contract ဆိုတာက ကၽြန္ေတာ္တို ့အသံုးၿပဳမဲ့ မက္ေဆ့ (message) ရဲ ့ ပံုစံ (format) ပါပဲ။ ဥပမာ - မက္ေဆ့ (message) ေတြဟာ headers ထဲမွာ ရိွရမလား။ ဒါမွမဟုတ္ body ထဲမွာပဲ ရိွရမလား။ မက္ေဆ့ ေတြအတြက္ ဘယ္လို လံုၿခံဳေရး နည္းလမ္း (security) ေတြ အသံုးၿပဳမလဲ စတာေတြကို သတ္မွတ္ေပးတဲ့ contract ပါ။

(၁၃) fault contract
fault contract ေတြဟာ service တစ္ခုမွာ အမွား (error) ေတြၿဖစ္လာတဲ့ အခါ ေခၚတဲ့သူဆီ၊ service ကို သံုးတဲ့သူဆီကို အဲဒီ အမွား ေတြ ပို ့ေဆာင္ေပးႏိုင္ေအာင္လို ့ service operation နဲ ့ ေပါင္းစပ္ထားႏုိင္ပါတယ္။ Operation တစ္ခုမွာ အမွား ေတြ မရိွတာလည္းၿဖစ္ႏိုင္သလို ့ အမ်ားၾကီးရိွေနတာလည္း ၿဖစ္နိုင္ပါတယ္။ ဒီ အမွားေတြ ကို SOAP (Simple Object Application Protocols) ရဲ ့ အမွားေတြၿဖစ္ၿပီး programming မွာေတာ့ Exception ေတြ အေနနဲ ့ ကိုင္တြယ္ပါတယ္။

(၁၄) data contract
Service တစ္ခုက အသံုးၿပဳမဲ့ ေဒတာ အမ်ိဳးအစား (data types) ေတြကို ေဒတာ၂ (metadata – data about data) ေတြမွာ ေၾကညာေပးရပါတယ္။ ဒါမွ service ကို အသံုးၿပဳမဲ့ အရာေတြက ဆက္သြယ္အသံုး ၿပဳႏိုင္မွာၿဖစ္တယ္။ အဲဒီ လို ေဒတာ အမ်ိဳးအစားေတြကို ေဖာ္ၿပေပးတာကို data contract လို ့ေခၚပါတယ္။ အဲဒီ ေဒတာအမ်ိဳးအစားေတြကို မက္ေဆ့ ရဲ ့ အစိတ္အပိုင္းတစ္ခုအေနနဲ ့ ထည့္ေပးႏိုင္ပါတယ။္ ဥပမာ - parameter အေနနဲ ့ ဒါမွမဟုတ္ return type အေနနဲ ့။ တကယ္လို ့ service က ရိုးရွင္းတဲ့ ေဒတာ အမ်ိဳးအစား (int, string,lone စသည္) ကို ပဲ အသံုးၿပဳမယ္ဆိုရင္ေတာ့ data contract ကို အထူးတလည္ ေၾကညာေပးစရာမလိုပါဘူး။

(၁၅) hosting
Service တစ္ခုဟာ process တစ္ခုေပၚမွာ လုပ္ေဆာင္ရပါတယ္။ အခံ process မရိွပဲ မရပါဘူး။ host တစ္ခုဆိုတာ application တစ္ခုၿဖစ္ၿပီး သူက service ရဲ ့ သက္တမ္း (lifetime) ကို သတ္မွတ္ ၿပဥာန္းေပးပါတယ္။ Service ေတြဟာ ကိုယ့္ကိုယ္ အခံ (host) လုပ္ထားတာလဲ ၿဖစ္ႏိုင္ပါတယ္။ ဒါမွမဟုတ္ အၿခား process တစ္ခုကို အခံ (host) လုပ္တာလည္းၿဖစ္ႏိုင္ပါတယ္။

(၁၆) self-hosted service
Self-hosted service ဆိုတာ developer တစ္ေယာက္က တည္ေဆာက္ထားတဲ့ process တစ္ခု ထဲမွာ လုပ္ေဆာင္ (run) ေနတဲ့ service ကို ေခၚတာၿဖစ္တယ္။ developer က အဲဒီ service ရဲ ့ သက္တမ္းကို ထိန္းခ်ဳပ္ထားတယ္၊ service ရဲ ့ properties ေတြကို ကိုင္တြယ္တယ္၊ service ကို ဖြင့္တယ္ ( service ဆီ ကို client က အ၀င္ message လာတာကိုေစာင့္ၿခင္း) ၊ ၿပီးေတာ့ service ကို ပိတ္တယ္။

(၁၇) hosting process
Hosting process ဆိုတာကေတာ့ application တစ္ခုၿဖစ္ၿပီး သူရဲ ့ ရည္ရြယ္ခ်က္က service တစ္ခုအတြက္ အခံ (host) လုပ္ေပးဖို ့ပါပဲ။ ဘာေတြပါလည္းဆိုေတာ့ - Internet Information Service (IIS), Window Activation Service (WAS), Windows Services ေတြပါပဲ။ ဒီနည္းလမ္းမွာ အခံ (host) ေတြက service ရဲ ့သက္တမ္းကို ထိန္းခ်ဳပ္ပါတယ္။ ဥပမာ - IIS ကိုသံုးၿပီး service assemblyနဲ ့ configuration file ပါတဲ့ virtual directory တစ္ခု တည္ေဆာက္ႏိုင္တယ္။ ၿပီးေတာ့ message ေတြ ေရာက္လာတဲ့ အခါ၊ IIS က service ကို စတင္ၿပီး သူရဲ ့ သက္တမ္းကို ထိန္းခ်ဳပ္ပါတယ္။

(၁၈) instancing
Service တစ္ခုမွာ instancing model တစ္ခုရိွပါတယ္။ instancing model ၃မ်ိဳးရိွတယ္။ single - ဒီမွာ single CLR (Common Language Runtime) object တစ္ခုကပဲ client အားလံုးကို လက္ခံ လုပ္ေဆာင္ေပးပါတယ္။ per call – ဒီမွာ CLR object အသစ္တစ္ခုကို client တစ္ခုဆီကို လက္ခံလုပ္ေဆာင္ဖို ့ အတြက္ ဖန္တီးေပးပါတယ္။ per session - ဒီမွာေတာ့ CLR object အစု တစ္စု ကို သီၿခား session တစ္ခုစီ အတြက္ ဖန္တီးေပးပါတယ္။ ဘယ္ ပံုစံကို သံုးမလဲဆိုတာကေတာ့ service ရဲ ့ အမ်ိဳးအစား အသံုးၿပဳမဲ့အေနအထားေပၚမူတည္ပါတယ္။

(၁၉) client application
Client application တစ္ခုဟာ program တစ္ခုၿဖစ္ၿပီး သူက မက္ေဆ့ (message) ေတြကို endpoints တစ္ခုကေန တစ္ခုကို ပို ့ေပး လက္ခံ ေပးတာကို လုပ္တယ္။ ဒီ client application ေတြဟာ WCF client instance တစ္ခုကို တည္္ေဆာက္ၿပီး WCF client method ကို ေခၚၿပီး အသံုးၿပဳတဲ့ အခါမွာ စတင္ၿဖစ္တည္ပါတယ္။ အေရးၾကီးတာ တစ္ခုကို သတိၿပဳရမွာက application တစ္ခုတည္းကပဲ client ေရာ service ေရာ ၿဖစ္ႏိုင္တယ္ဆုိတာပါ။

(၂၀) WCF client
WCF client ဆိုတာ service operation ေတြကို methods ေတြအၿဖစ္ တင္ၿပေပးတဲ့ client-application တစ္ခုပါပဲ။ ဘယ္ application ေတြမဆို service ကို အခံ (host) လုပ္ေပးတဲ့ ဟာ အပါအ၀င္ WCF client ေတြကို အခံ (host) အၿဖစ္လုပ္္ေပးႏိုင္ပါတယ္။ ဒါေၾကာင့္ service တစ္ခုဟာ အၿခား service တစ္ခုရဲ ့ WCF Client ေတြပါရိွႏိုင္ပါတယ္။
WCF client ကို ServiceModel Metadata Utility Tool (Svcutil.exe) ကို သံုးၿပီး အလိုအေလၽႊွ်ာက္ ထုတ္ယူႏိုင္ပါတယ္။
.
(၂၁) metadata
Service တစ္ခုရဲ ့ metadata ေတြက အဲဒီ service ရဲ ့ အခ်က္အလက္ေတြကို ေဖာ္ၿပေပးပါတယ္။ အဲဒီ အခ်က္အလက္ေတြကို သံုးၿပီးမွာ အၿပင္က client ေတြက service ကို ဆက္သြယ္လုပ္ေဆာင္ႏိုင္မွာ ၿဖစ္တယ္။ အဲဒီ metadata ကိုေတြကို ရယူအသံုးၿပဳၿပီး serviceModel Metadata Utility Tool (svcutil.exe) က WCF client ေတြနဲ ့ လိုအပ္တဲ့ configuration file ေတြကို service နဲ ့ ဆက္သြယ္လုပ္ကိုင္ဖို ့ ထုတ္ေပးႏိုင္တာၿဖစ္တယ္။ service တစ္ခုအတြက္ metadata က ထုတ္ေပးတဲ့ ေဒတာက XML Schema documents ရယ္၊ WSDL document တို ့ ၿဖစ္တယ္။

(၂၂) Security
WCF ရဲ ့ Secuity ဆိုတာ confidentiality ရိွၿခင္း၊ (မက္ေဆ့ ေတြကို ပံုဖ်တ္ (encryption) လုပ္ၿခင္း)၊ integrity ရိွၿခင္း၊ (မက္ေဆ့ေတြ ပ်က္ဆီးတာမ်ိဳးကို နားလည္ၿခင္း)၊ authentication (serviers နဲ ့ clients တို ့မွာ ၀င္ႏိုင္ခြင့္ ရိွၿခင္း)၊ authorization (အရင္းအၿမစ္ (resource) ေတြကို သံုးခြင့္ ရိွၿခင္း) စတာေတြကို ေၿပာတာၿဖစ္တယ္။ ဒီ လို လုပ္ေဆာင္ခ်က္ေတြကို လက္ရိွ ရိွၿပီးသား လံုးၿခံဳေရး (security) ေတြၿဖစ္တဲ့ TLS ၊ HTTP (HTTPS)၊ တို ့ကေနလုပ္ေဆာင္ႏိုင္သလို၊ အသစ္ၿဖစ္တဲ့ WS-* ရဲ ့ လံဳၿခံဳးေရး စံညြန္းသတ္မွတ္ခ်က္ (security specification) ကေနလည္းလုပ္ေဆာင္ႏိုင္ပါတယ္။

WS-*

ဒါကေတာ့ Web Service (WS) ရဲ ့ စံညြန္းသတ္မွတ္ခ်က္ေတြကို ညြန္းပါတယ္။ WS-Security WS-ReliableMessaging စသည္ၿဖင့္ WCF မွာ သံုးရမဲ့ သတ္မွတ္ခ်က္ေတြပါ။

ဒါေလးကေတာ့ မိတ္ဆက္ သေဘာပါပဲ။
အခ်ိ္န္ရရင္ ဆက္ၿပီးေရးပါမယ္။
ဆက္ၿပီး ေလ့လာႏိုင္ၾကပါေစဗ်ာ။

ရည္ညြန္း။ ။ MSDN

အာေကလာ
၁၈ စက္တင္ဘာ ၂၀၀၈



Saturday, September 13, 2008

Javascript toLocaleString() Method

အဓိပၸါယ္ဖြင့္ဆိုခ်က္

toLocaleString method ေလးက Date object တခုကို လက္ရိွ စက္ရဲ ့ အခ်ိန္ ေပၚမူတည္ၿပီး String အၿဖစ္ ၿပန္ေပးပါတယ္။

အသံုးၿပဳပံု

ေအာက္က နမူနာမွာ (စက္ထဲမွာရိွေနတဲ့) လက္ရိွ ဒီေန ့အခ်ိန္ကို stirng အၿဖစ္ ၿပန္ထုတ္ရိုက္ၿပပါလိမ္မယ္။


<script type="text/javascript">
var todayDateObj = new Date();// var dobDateObj = new Date('Feb 23, 1983');
var todayDateStr = todayDateObj.toLocaleString();// var dobStr = dobDateObj.toLocaleString();
document.write(todayDateStr);//document.write(dobStr);
alert(todayDateStr);//alert(dobStr);
</script>

ေအာက္မွာ ၿပထားသလိုမ်ိဳး (သံုးေနတဲ့ စက္ရဲ ့ ပံုစံခ်ပံု (setting) ေပၚမူတည္ပါတယ္။) ဘေရာက္ဇာ ကေနၾကည့္ရင္ ၿမင္ရပါလိမ့္မယ္။





ရည္ညြန္း။ ။ w3school

အာေကလာ။

Wednesday, August 27, 2008

XML Schemas
(XML သဒၵါ (၀ါ) XML အဓိပၸါယ္ သတ္မွတ္မွဳ) - အပိုင္း ( ၅ )

အပိုင္း ( ၁ ) ၊
( ၂ ) ၊ (၄) ႏွင့္ ( ၅) ကို အရင္ဖတ္ၾကည့္ေစခ်င္ပါသည္။



Content model ေတြဖန္တီးဖို ့အတြက္ <simpleType> element ကိုအသံုးၿပဳပါတယ္။ ဒါေပမယ့္ အဲဒီ content model ေတြဟာ simple content ၿဖစ္ေနဆဲပါပဲ။ simple content ဆိုတာ data ပဲပါၿပီး child elements ဒါမွမဟုတ္ mixed contents ေတြ မပါတဲ့ ဟာပါ။ simple content ၿဖစ္ေနဆဲေပမယ့္ <simpleType> က ၿပဳလြယ္ၿပင္လြယ္၊ ေၿပာင္းလဲလြယ္တဲ့ အဆင့္တစ္ခုကို ၿဖစ္ေစပါတယ္။ (a level of flexibility) လက္ရိွ ရိွေနတဲ့ ေဒတာ အမ်ိဳးအစား ေတြမွာ ကန္ ့သတ္ခ်က္ေတြ ၿပဳလုပ္ႏိုင္တာမ်ိဳးေပါ့။

ဘာေၾကာင့္ အဲဒီ ကန္ ့သတ္ခ်က္ေတြက အသံုး၀င္ရတာလဲလို ့ ေမးခဲ့ရင္ ဒါေလးကို ေတြးၾကည့္ၾကမယ္။ ကၽြန္ေတာ္တို ့ပစၥည္း တစ္ခုအတြက္ ကုန္စရင္း (inventory) ကို element တစ္ခုေၾကညာၾကမယ္။ ဒီေတာ့ <inventory> ဆိုတဲ့ element ေလးကို ကၽြန္ေတာ္တုိ ့ ဖန္တီးမယ္ေပါ့။ ဒါေပမယ့္ ကၽြန္ေတာ္တို ့ ၾကိဳသိေနတယ္။ ဒီ ပစၥည္း ဟာ အခု ၂၀၀ ထက္ပိုၿပီး သိုေလွာင္ရံုထဲမွာမရိွႏိုင္ဘူးဆိုပါစို ့။ လံုး၀မရိွတာက်ေတာ့ ၿဖစ္ႏိုင္တယ္။ ဒီလို ကန္ ့သတ္ခ်က္ေလးေတြ ပါလာၿပီဆိုရင္၊ ေနာက္ၿပီး ဒီလုိ ဟာေလးေတြက မၾကာခဏ သံုးရေတာ့မယ္၊ ထပ္ခါထပ္ခါသံုးရေတာ့မယ္ဆိုရင္ ကၽြန္ေတာ္တို ့ဟာ <simpleType> ကို သံုးၿပီး XML သဒၵါ (Schema) ကုိေၾကညာသင့္ၿပီေပါ့။ ေအာက္မွာၾကည့္မယ္။

<xs:element name="inventory">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="200"/>
</xs:restriction>
</xs:simpleType>
</xs:element>


အဲဒီ ေၾကညာခ်က္ေလးက <inventory> လို ့ေခၚတဲ့ element တစ္ခုကို ၿဖစ္ေစတယ္။ သူမွာ တန္ဖိုး (value) က 0 ကေန ၂၀၀ အထိအၿမင့္ဆံုးပါႏိုင္ပါတယ္။
(တခု သတိၿပဳပါ။ ကၽြန္ေတာ္တို ့သံုးတဲ့ ေဒတာအမ်ိဳးအစားဟာ XML မွာ ရိွၿပီးသား ေဒတာအမ်ိုးအစား ဆိုတာပါပဲ။ interger အမ်ိဳးအစားေပါ့။ ဒါကို ကၽြန္ေတာ္တို ့က restriction ဆိုတာနဲ ့ကန္ ့သတ္ရံုပဲ ကန္ ့ သတ္လိုက္တာပါ။)

ဒီလိုေၾကညာခဲ့တဲ့ အတြက္ တကယ္လို ့ XML Document ဖိုင္ထဲမွာ ဒီလို တက္(tag) ကေလးေတြပါလာရင္ အိုေကၿပီး
<inventory>190</inventory>
<inventory>0</inventory>
<inventory>49</inventory>


ဒီလိုေလးေတြေတာ့ ပါလို ့မရဘူးေပါ့။
<inventory>201</inventory>
<inventory>250</inventory>


<simpleType> element ကို ေၾကညာတဲ့ အခါေတြမွာ restriction ရယ္၊ list ရယ္၊ union ရယ္ပဲ ပါလို ့ ရပါတယ္။

ထပ္သိထားရမွာက <simpleType> element မွာ id နဲ ့ name ဆိုတဲ့ စြမ္းရည္ၿပစကားလံုး (attributes) ေလးေတြပါလို ့ရတယ္။ ဒီလို simple type တစ္ခုကို name သတ္မွတ္ေပးလို ့ရတဲ့ အတြက္ အက်ိဳးေက်းဇူးကေတာ့ ကၽြန္ေတာ္တို ့ဟာ အၿခား element ေတြေၾကညာတဲ့ အခါမွာ အဲဒီ type ကို အဲဒီ name ကိုအသံုးၿပဳ ၿပီး ၿပန္ေခၚသံုးလို ့ ရၿခင္းပါပဲ။ ထပ္ခါထပ္ခါ အသံုးၿပဳႏိုင္ၿခင္း ( reusable) ၿဖစ္တာေပါ့။

<xs:simpleType name="currency">
<xs:restriction base="xs:decimal">
<xs:fractionDigits value="2"/>
</xs:restriction>
</xs:simpleType>

<xs:element name="price" type="currency"/>


အေပၚက နမူနာမွာ ကၽြန္ေတာ္တို ့ဟာ price လို ့ေခၚတဲ့ element တခုရိွတယ္။ အဲဒီ price ဟာ decimal အမ်ိဳးအစားၿဖစ္ၿပီး ေတာ့ ဒသမဂဏန္း ၂ လံုးပဲ ပါလို ့ရမယ္။

ဒီေတာ့ ကၽြန္ေတာ္တို ့ဟာ decimal ကို base ေဒတာအမ်ိဳးအစားအေနနဲ ့ယူလိုက္ၿပီး သူ ရဲ ့ဒသမဂဏန္းကို xs:fractionDigits တက္(tag)က တန္းဖိုး 2 ဆိုၿပီး ေၿပာင္းလိုက္တယ္။ တကယ္ decimal ေဒတာအမ်ိဳးအစားဟာ ဒသမ ၂ လံုးပဲ ပါတာမဟုတ္ဘူး။ ဒါေၾကာင့္ ကၽြန္ေတာ္တုိ ့က ဒီလို ေၿပာင္းၿပီးေတာ့ မွ ကၽြန္ေတာ္တို ့ေၿပာင္းထားတဲ့ အမ်ိုးအစား (type) ကို currency လို ့ simpleType ရဲ ့ name မွာ ေၾကညာလိုက္တယ္။ ဒသမဂဏန္း ၂ လံုးနဲ ့ဟာကို currency လို ့ နာမည္ေပးလိုက္တယ္ေပါ့ဗ်ာ။

ၿပီးေတာ့ XML Document ထဲမွာ အဲဒီ နာမည္ကို ယူၿပီး ဒီလို သံုးလိုက္လို ့ရပါၿပီ။

<xs:element name="price" type="currency"/>


တကယ္လို ့ အၿခား element တခု ၊ ဥပမာ- totalAmount ဆိုတဲ့ element ကလည္း price လိုပဲ ဒသမဂဏန္း ၂ လံုးဆိုရင္-

<xs:element name=”totalAmount” type="currency"/>


paymentAmount ကလည္း ဒသမ ၂ လံုးပဲ ဆိုပါစို ့။

<xs:element name=”paymentAmount” type="currency"/>


ဆိုၿပီး သံုးလိုရပါတယ္။ ေတြ ့လား။ currency ဆိုတဲ့ ေဒတာအမ်ိဳးအစားကို ပဲ ေခၚသံုးထားတယ္။ တခါတည္း simpletype အေနနဲ ့ ေၾကညာၿပီး ထပ္ေၾကညာစရာမလို ေတာ့ဘူး။ မဟုတ္ရင္ element အသစ္တစ္ခု ဖန္တီးတိုင္း ဒသမ ၂ လံုးဆိုတာကို ေၾကညာေနရမွာ။ အခု မလိုေတာ့ဘူး။ currency ဆိုတဲ့ name ကို ေခၚသံုးရံုပဲ။ ဒါကို ထပ္ခါထပ္ခါ အသံုးၿပဳႏိုင္ၿခင္း ( reusable) ၿဖစ္တယ္လို ့ေခၚတယ္။ ဘယ္ element ကမဆို သူတို ့ရဲ ့ ေဒတာအမ်ိုးအစား (data type ) အတြက္ သင့္ေလွ်ာ္တယ္ဆိုရင္ တၿခား XML ရဲ ့ ရိွၿပီးသားေဒတာ (build in datatype – eg: Int, Decimal , etc..) ေတြလိုပဲ ေခၚသံုးႏိုင္တယ္။

(သတိၿပဳရန္- type=”currency” ဆိုတာ XML Build-in DataType( XML မွာ အလိုအေလွ်ာက္ပါၿပီးသား ေဒတာအမ်ိုးအစား) မဟုတ္ဘူး။ ကၽြန္ေတာ္တို ့ အေပၚမွာ <simpleType> ဆိုၿပီးေက်ညာခဲ့လို ့သံုးလို ရတာ။ သူဟာ ေဒတာ အမ်ိဳးအစားတစ္ခုလို ့ၿဖစ္သြားပါတယ္။ အဲဒါကို အဓိကေၿပာခ်င္တာပါပဲ။)

ဒါဟာ XML သဒၵါ (schema) ရဲ ့ အလြန္ေကာင္းတဲ့ အားသာခ်က္တစ္ခုပါပဲ။

<simpletype> မွာ ကၽြန္ေတာ္တို ့ <list> <restriction> < union> ေတြကိုသံုးလို ့ ရတယ္လို ့ ေၿပာခဲ့ပါတယ္။
အေပၚမွာ <restriction> ကို သံုးၿပသြားခဲ့ပါတယ္။ အခု <list> ကိုသံုးၿပီး ဘယ္လို ့ အားသားခ်က္ေတြရိွလဲ၊ ဘယ္လိုကိစၥေတြကို လုပ္လို ့ရလဲ ဆိုတာၾကည့္ရေအာင္။

List

List က တန္ဖိုးတစ္ခုတည္းထက္ တန္ဖိုးအမ်ားၾကီးကိုလက္ခံႏိုင္တဲ့ ကိုယ္ပိုင္ ေဒတာ အမ်ိဳးအစားတစ္ခုကို ဖန္တီးတာကို ေၾကညာခ်င္တဲ့အခါမွာ သံုးလို ့ရပါတယ္။ ဥပမာ - ကၽြန္ေတာ္တို ့က color ဆိုတဲ့ element ေလးတစ္ခုကို တည္ေဆာက္မယ္ဆိုပါစို ့။ အဲဒီ ေလးဟာ မွာ အနီ (red) ၊ အ၀ါ (yellow) ၊ အၿပာ (blue) ၊ အစိမ္း (green) စသည္ၿဖင့္ ပါမယ္ေပါ့။ အဲဒီအခါ ကၽြန္ေတာ္တို ့က XML သဒၵါ (XML schema) မွာ ဒီလုိေၾကညာေပးလို ့ရတာေပါ့။

<xs:element name=”color” type=”xs:string”></xs:element>


ဒါေပမယ့္ ဒီလိုေၾကညာရင္ ကၽြန္ေတာ္တို ့ရဲ ့ <color> မွာ တန္ဖိုး (value) တစ္ခုပဲပါလို ့ရမယ္။ ဒီလိုမ်ိဳး -
<color>red</color>  (ဒါက အနီ)
<color>green</color> (ဒါက အ၀ါ အဲလို တစ္ခုမွာ တန္ဖိုး တခုပဲပါလို ့ရမယ္)


တကယ္လို ့ ကၽြန္ေတာ္တို ့က <color> မွာ တန္ဖိုးေတြ တစ္ခုထက္မက ပါေစခ်င္တယ္ဆိုပါစို ့။<list> ကို သံုးၿပီးလုပ္လို ့ ရၿပီေပါ့။

<xs:element name=”color”>
<xs:simpleType>
<xs:list itemType=”xs:string”>
<xs:restriction>
<xs:minLength value=”1”/>
<xs:maxLength value=”5”/>
</xs:restriction>
</xs:list>
</xs:simpleType>
</xs:element>


ဒီ XML Schema မွာ <simpleType> ထဲမွာ <list> ကိုထည့္ၿပီး ေၾကညာထားပါတယ္။ အဲဒီ <list> မွာ itemType ဆိုတဲ့ စြမ္းရည္ၿပ စကားလံုး (attribute) တစ္ခုလည္းသံုးထားတယ္။ သူက ဘာကိုေၿပာတာလည္းဆိုေတာ့ ဒီ list ထဲမွာ ပါမဲ့ ေဒတာအမ်ိဳးအစားကို ေၿပာၿပတယ္။ ဒီမွာဆို xs:string ၊ (စာအစဥ္အတန္း) ေဒတာအမ်ိဳးအစားေပါ့။ ဟုတ္တယ္ေလ။ ပါမဲ့ တန္ဖိုးက Red, Blue, Green စသည္ဆိုေတာ့ string အမ်ိဳးအစား ေဒတာေပါ့။ ၿပီးေတာ့ ကၽြန္ေတာ္တုိ ့ အေပၚက သံုးဖူးတဲ့ <restriction> ကို ၿပန္သံုးတယ္။ ဒီေနရာမွာေတာ့ ကၽြန္ေတာ္တို ့က <minLength> နဲ ့ <maxLength> တက္(ဂ္) ေလးကို သံုးထားတယ္။ အဲဒီ တက္(ဂ္)ေလး ၂ ခုက ဘာကိုေၿပာလည္းဆိုေတာ့ <color> element ထဲမွာ ပါမဲ့ တန္းဖိုး အေရအတြက္။ အခု <list> ကိုသံုးလိုက္ေတာ့ <color> ထဲမွာ တန္ဖိုးက တစ္ခုတည္းမဟုတ္ေတာ့ဘူးေလ။ တခုထက္မက ပိုၿပီးပါလာနိုင္ၿပီ မဟုတ္လား။ ဒီေတာ့ ကၽြန္ေတာ္တို ့က min နဲ ့ max ကိုသံုးၿပီး အႏွိမ့္ဆံုး ဘယ္ႏွစ္ခု၊ အၿမင့္ဆံုး ဘယ္ႏွခု ပါခြင့္ရိွႏိုင္သလဲဆိုတာကို သတ္မွတ္ေပးလိုက္တယ္။ ဒီမွာဆို အႏွိမ့္ဆံုး ၁ ခု၊ အၿမင့္ဆံုး ၅ ခုပါခြင့္ ရိွတယ္ေပါ့။

သတိ။ <minLength> နဲ ့ <maxLenght>တို ့က string (စာအစဥ္အတန္း) တစ္ခုလံုးရဲ ့ အရွည္ကို ေၿပာတာမဟုတ္ပါ။ <list> ရဲ ့ အရွည္ (list ထဲမွာ ဘယ္ႏွခုပါလည္းဆိုတာ) ကိုေၿပာတာပါ။

ဒီေတာ့ ၾကည့္ၾကမယ္။

<color>Red Blue Green</color> ဒါဆို အားလံုးအိုေကေပါ့။ Red, Blue, Green ဆိုမွ List က ၃ ခုပဲရိွေသးတာပဲ။ ဟုတ္။

<color>Red Blue Green Black Orange Purple Brown</color> ဒီလိုေတာ့ မရဘူးေပါ့။ ဘာလို ့ လည္းဆိုေတာ့ အၿမင့္ ဆံုး ရိွရမယ့္ အရွည္ ထက္ ပိုမ်ားေနတယ္ေလ။ အၿမင့္ဆံုးအရွည္က ၅ ၊ အခုက ၇ ခု၇ိွေနတယ္။

<color></color> ဒီလိုလည္းမရဘူး။ အနည္းဆံုး တန္းဖိုး ၁ ခုပါရမယ္လို ့သတ္မွတ္ထားခဲ့တယ္။ မွတ္မိတယ္ထင္ပါတယ္။

<list> က ကၽြန္ေတာ္တို ့ element ထဲမွာ တန္ဖိုးတစ္ခုထက္ပိုၿပီး ထည့္ေပးခ်င္တဲ့ အခါ၊ အဲလို element ကို schema (သဒၵါ) မွာ ေၾကညာရတဲ့ အခါမ်ိဳး အသံုးက်ပါတယ္။

ဆက္ရန္။

ထပ္ေရးေနပါတယ္ဗ်ာ..။ :D

အာေကလာ (akela)

Aug 27, 2008 12.02 pm

Monday, December 10, 2007

XML Schemas
(XML သဒၵါ (၀ါ) XML အဓိပၸါယ္ သတ္မွတ္မွဳ) - အပိုင္း ( ၄ )

အပိုင္း ( ၁ ) ၊
( ၂ ) ႏွင့္ ( ၃) ကို အရင္ဖတ္ၾကည့္ေစခ်င္ပါသည္။

Content Models

တခါတေလမွာ Empty element ေတြဟာ အေရးပါအရာေရာက္ေပမယ့္ မ်ားေသာအားၿဖင့္ တန္ဖိုး (value) ရိွတဲ့ element ေတြပဲမ်ားပါတယ္။ element တစ္ခုဟာ ရိုးစင္းတဲ့ content အမ်ိဳးအစားေတြရိွတယ္။ number တို ့ string တို ့လိုမ်ိဳးေပါ့။ ဒါမွမဟုတ္ စာေတြတၿခား element ေတြ ေရွာေထြးေနတာမိ်ဳးလည္း ၿဖစ္တက္တယ္။

element တစ္ခုဟာ သူ ့ေအာက္မွာ အၿခား element မရိွဘဲ တန္ဖိုး (value) ပဲရိွတယ္ဆိုရင္ သူကို ရိုးရွင္းတဲ့ content (simple content) လို ့ေခၚတယ္။တကယ္လို ့ element တစ္ခုဟာ သူကိုယ္တိုင္က parent element ၿဖစ္ၿပီး သူ ့ေအာက္မွာ child element ေတြရိွတယ္ဆိုရင္ သူ ့ကို element content လို ့ေခၚတယ္။ ေနာက္ဆံုး တစ္ခုအေနနဲ ့ စာေတြေရာ ၊ child element ေတြေရာ ေရာေထြးၿပီးရိွေနတယ္ဆိုရင္ သူကို mixed content လို ့ေခၚတယ္။ ဒါက ဘယ္အေၿခအေနမွာ ဘယ္လို သံုးႏွန္းတာလည္းဆိုတာကို ေၿပာတာပါ။

element တစ္ခုကို တည္ေဆာက္တဲ့ အခါ အရိုးရွင္းဆံုးနည္းကေတာ့ built in data type (XML မွာ အလိုအ ေလွ်ာက္ပါၿပီးသား data အမ်ိဳးအစား) ကို သံုးတာၿဖစ္ပါတယ္။အဲဒီထဲမွာ string, decimal, date, Boolean, နဲ ့ time တို ့ပါ၀င္ၾကပါတယ္။ data အမ်ိဳးအစားက လည္း built in၊ content ကလည္း ရိုးရွင္းတဲ့ ေဒတာ (simple data) ဆိုရင္ stand alone element declaration ကို သံုးလို ့ရပါတယ္။ ဥပမာ -

<xs:element name="quantity" type="xs:interger"/>

ဒီ ေၾကညာခ်က္ေလးက <quantity> ဆိုတဲ့ element တစ္ခုကို အသံုးၿပဳႏိုင္ေအာင္လုပ္ေပးပါလိမ္မယ္။ ေဒတာ အမ်ိဳးအစားကေတာ့ interger ေပါ့။

<quantity>5</quantity>

ေဒတာအမ်ိဳးအစာ decimal ၊ temperature element အတြက္ဆိုရင္

<xs:element name="temperature" type="xs:decimal"/>

XML ဖိုင္ထဲမွာေတာ့ ဒီလိုသံုးလို ့ရေရာေပါ့။

<temperature>9.89</temperature>

ေဒတာအမ်ိဳးအစာ date၊ holiday element အတြက္ဆိုရင္

<xs:element name="holiday" type="xs:date"/>

XML ဖိုင္ထဲမွာေတာ့ ဒီလိုသံုးလို ့ရေရာေပါ့။

<holiday>2007-12-13</holiday>

date ေဒတာအမ်ိဳးအစားက YYYY-MM-DD(ႏွစ္၊ လ၊ ရက္) ကို သတ္မွတ္ေပးပါတယ္။

ေဒတာအမ်ိဳးအစာ boolean ၊ attendenct element အတြက္ဆိုရင္

<xs:element name="attendenct" type="xs:boolean"/>

XML ဖိုင္ထဲမွာေတာ့ ဒီလိုသံုးလို ့ရေရာေပါ့။

<attendenct>true</attendenct>

ေဒတာအမ်ိဳးအစာ string ၊ title element အတြက္ဆိုရင္

<xs:element name="title" type="xs:string"/>

XML ဖိုင္ထဲမွာေတာ့ ဒီလိုသံုးလို ့ရေရာေပါ့။

<title>XML Schema Introduction</title>

အဲဒီလို ေပါ့။ ဟုတ္လား။
ပံုစံ ထုတ္ၾကည့္ရင္ ဒီလို ေၿပာလို ့ရမယ္။

<xs:element name="element" type="xs:data_type"/>

element ေတြကို ဒီလို ေၾကညာတာဟာ simple content (အထဲမွာ data ပဲပါၿပီး ဘာမွမပါတာ) ေတြအတြက္ဆို အလုပ္လုပ္ပါ တယ္။ ဒါေပမယ့္ တကယ္လက္ေတြ မွာေတာ့ အဲဒီလို ့ string, date , decimal , boolean ေဒတာေလးေတြ ပဲ ပါတာ မဟုတ္ဗ်။ ပိုၿပီးေတာ့ ရွုပ္ေထြးတယ္။ ဒါေၾကာင့္ content model ဆိုၿပီးၿဖစ္လာရတယ္။ ေအာက္ကေတာ့ အဲဒီအေၾကာင္းပါပဲ။

<simpleType>

ဆက္ရန္။(ေနာက္ေန ့မွ ဒီပိုစ္မွာပဲ ဆက္ေရးပါမယ္)။

အာေကလာ(akela)
Dec 10, 2007 09:53 pm





Wednesday, December 5, 2007

temp Explaination




namespace testingWebLinks

{
class Program
{
static void Main(string[] args)
{
string line;

Dictionary<string,string> dictionaryOutLinks = new Dictionary<string,string>();
သူက page တစ္ခုစီမွာ out links ဘယ္ႏွခုရိွလည္း ကို သိမ္းမွာ။
Dictionary<string,string> dictionaryInLinks = new Dictionary<string,string>();
သူက page တစ္ခုစီမွာ in links ဘယ္ႏွခုရိွလည္း ကို သိမ္းမွာ။
Dictionary<string,string> dictionaryOutPages = new Dictionary<string,string>();
သူက page တစ္ခုစီမွာ ဘယ္ out page ေတြဆီိကို ထြက္သြားတာလည္းကို သိမ္းမွာ။
Dictionary<string,string> dictionaryInPages = new Dictionary<string,string>();
သူက page တစ္ခုစီမွာ ဘယ္ in page ေတြက ၀င္လာတာလည္းကို သိမ္းမွာ။

dictionaryInLinks.Clear();
dictionaryOutLinks.Clear();
dictionaryInPages.Clear();
dictionaryOutPages.Clear();
ဒါကေတာ့ စစခ်င္း clear လုပ္တာပါ။ အေရးမၾကီးပါ။

// Read the file and display it line by line.
String visitedString = "Visited:";
သူက ငါတို ့ဖတ္မဲ့ link က visited link ဟုတ္လား မဟုတ္လားကို စစ္ဖို ့။
String curVisitStr = String.Empty;
သူကေတာ့ လက္ရိွ visited link ကို သိမ္းထားဖို ့ (program အရ လိုေနလို ့ပါ။ေလာဂ်စ္အရ အေရးမၾကီး)။

Int32 inCounter = 0; in links စုစု ေပါင္း
Int32 outCounter = 0; out link စုစုးေပါင္း
Boolean isInVisited = false; သူက မလို ပါဘူး။

System.IO.StreamReader file = new System.IO.StreamReader("C:\\Documents and Settings\\nhyan\\Desktop\\weblinks.txt");
ငါတို ့.. web log file ကို ဖတ္လိုက္တာ။
try
{
while ((line = file.ReadLine()) != null) တစ္ေၾကာင္းခ်င္းloop ပတ္တယ္။
{
if (line.StartsWith(visitedString))ဖတ္လိုက္တဲ့ link က visited နဲ ့ စတာလားကို စစ္
{
if (outCounter != 0) ပထမဆံုး visited link လား ေနာက္မွ ေတြ ့တဲ့ visited link လားစစ္
{
dictionaryOutLinks[curVisitStr.Trim()] = outCounter.ToString();
outCounter = 0; isInVisited = false;
}
dictionaryOutLinks.Add(line.Trim(), outCounter.ToString());
dictionaryOutPages.Add(line.Trim(), String.Empty);
ပထမဆံုး visited link ကို သိမ္းတယ္။သူရဲ ့ counter က ေလာေလာဆယ္ 0 ေပါ့။
curVisitStr = line.Trim();
curVisitStr ထဲကို လက္ရိွ link ကို ထည့္တယ္။
isInVisited = true; ဒါကမလို
}
else ဖတ္လိုက္တဲ့ link က visited နဲ ့ စတာမဟုတ္ဘူးဆိုရင္
{
if (isInVisited == true)ဒါလည္း မလို
{
if (!dictionaryInLinks.ContainsKey(line.Trim()))
In links dictionary ထည္းမွာ လက္ရိွ link ဟာ ရိွပီးလား ကို စစ္( မ၇ိွေသးဘူးဆိုရင္)
{
inCounter++; in coutner ကို ၁ တိုး
dictionaryInLinks.Add(line.Trim(), inCounter.ToString());
In links dictionary ကို လက္ရိွ Link က key၊ in counter က value အၿဖစ္ သိမ္း
outCounter++;
out counter ကိုလည္း ၁ တိုး.. ဒါက တစ္ေၾကာင္းဖတ္တိုင္း တိုးရမွာေလ ။ အခုက out link ကို ဖတ္ေနတာ
dictionaryInPages.Add(line.Trim(), curVisitStr);
in page dictionary ကို လည္း လက္၇ွိဖတ္ေနတဲ့ link ရယ္၊ အေပၚက visited link ၇ယ္ကို key value အၿဖစ္သိမ္း
}
else
In links dictionary ထည္းမွာ လက္ရိွ link ဟာ ရိွပီးလား ကို စစ္( ရိွတယ္ဆိုရင္) ရိွပီးသားကို update လုပ္ရမယ္ေလ အသစ္ထည့္လို ့မရေတာ့ဘူး
{
String tempValue = String.Empty;
String tempString = String.Empty;

// in link dictionar ကို key လက္၇ိွ link ေပးပီ ေတာ့ value ကို ထုတ္ယူ
dictionaryInLinks.TryGetValue(line.Trim(), out tempValue);
inCounter = Convert.ToInt32(tempValue);
inCounter++;
အဲဒီ value ကို ၁ တိုး
dictionaryInLinks[line.Trim()] = inCounter.ToString();
ၿပန္ပီးေတာ့ ထည့္

//dictionaryInLinks.Remove(line);
//dictionaryInLinks.Add(line, inCounter);

//တဆက္တည္း in page ပါ update လုပ္သြား
dictionaryInPages.TryGetValue(line.Trim(), out tempValue);
လက္ရိွ link ကို key အၿဖစ္ ေပးပီးေတာ့ထုတ္ယူ
tempString = tempValue.Trim() + "::" + curVisitStr.Trim();
value ကို update လုပ္
dictionaryInPages[line.Trim()] = tempString;
ၿပန္ထည့္

outCounter++;
ဒါကေတာ့ out counter ကို ၁ တိုးရမယ္ေလ။
}
String tempOutValue = String.Empty;
dictionaryOutPages.TryGetValue(curVisitStr.Trim(), out tempOutValue);
tempOutValue = tempOutValue + "::" + line.Trim();
dictionaryInLinks[curVisitStr.Trim()] = tempOutValue;
}
}
}

ဒီေအာက္ပိုင္းက.. dictionary ထည္းမွာ သိမ္းထားတာေတြကို txtဖိုင္မွာ ၿပန္၇ိုက္ထုတ္တာ။
Int32 i = 0;
TextWriter twOutLink = new StreamWriter("C:\\Documents and Settings\\nhyan\\Desktop\\outLinks.txt");
foreach (KeyValuePair kvp in dictionaryOutLinks)
{
if (i == 0)
{
// write a line of text to the file
twOutLink.Write("OUT LINKS");
twOutLink.WriteLine("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t PAGES");

i = 1;
}

twOutLink.WriteLine("{0} \t\t\t\t {1}",
kvp.Key, kvp.Value);
}
// close the stream
twOutLink.Close();

Int32 j = 0;

TextWriter twInLink = new StreamWriter("C:\\Documents and Settings\\nhyan\\Desktop\\inLinks.txt");
foreach (KeyValuePair kvp in dictionaryInLinks)
{
if (j == 0)
{
// write a line of text to the file
twInLink.Write("OUT LINKS");
twInLink.WriteLine("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t PAGES");

j = 1;
}

twInLink.WriteLine("{0} \t\t\t\t {1}",
kvp.Key, kvp.Value);
}
// close the stream
twInLink.Close();
}
catch (KeyNotFoundException exp)
{
Console.Write(exp.ToString());
}

// Suspend the screen.
Console.ReadLine();
}
}
}

Tuesday, December 4, 2007

XML Schemas
(XML သဒၵါ (၀ါ) XML အဓိပၸါယ္ သတ္မွတ္မွဳ) - အပိုင္း ( 3 )

အပိုင္း ( ၁ ) ႏွင့္ ( ၂ ) ကို အရင္ဖတ္ၾကည့္ေစခ်င္ပါသည္။

id
ဒီ id စြမ္းရည္ၿပစကားလံုး (attribute) ေလးက ကၽြန္ေတာ္တို ့ကို ဘာလုပ္ခြင့္ ၿပဳလဲဆိုေတာ့၊ element တစ္ခုအတြက္ ID (Identity) ကို ေၾကညာခြင့္ၿပဳပါတယ္။ ဒီ id ရဲ ့ တန္ဖိုး (value) ဟာ element ရဲ ့ Unique Key (ထပ္တူမရိွေသာ တန္ဖိုး) အၿဖစ္ လုပ္ေဆာင္ေပးပါတယ္။

maxOccurs
ဒီ maxOccurs က XML Document ထဲမွာ Element တစ္ခုဟာ ဘယ္ႏွစ္ၾကိမ္ ပါ၀င္ခြင့္ ရိွတယ္ဆိုတာကို သတ္မွတ္ေပးပါတယ္။ ဥမပါ schemas မွာ ဒီလို ေၾကညာထားမယ္ဆိုပါစို ့-

<element name="part" maxOccurs="3"/>


ဒါဆို XML Document ထဲမွာ part element ေလးဟာ အမ်ားဆံုး 3 ၾကိမ္ ပါလို ့ရပါတယ္။ (ေအာက္မွာၾကည့္ပါ)။

<car>
<part>wheel</part>
<part>body</part>
<part>roof</part>
</car>


မရိွသံုးတန္ဖိုး(default value) အေနနဲ ့ maxOccurs ရဲ ့တန္ဖိုးဟာ 1 ၿဖစ္ပါတယ္။ အႏွတ္ကိန္းမဟုတ္တဲ့ တန္ဖိုးမ်ားကို လက္ခံပါတယ္။ အဲလို ဂဏန္း (interger) ေတြမဟုတ္ပဲ "unbounded" လို ့ တန္ဖိုးသတ္မွတ္ခဲ့ ရင္ element တစ္ခုဟာ ၾကိဳက္သေလာက္ ပါႏိုင္တယ္လို ့အဓၶိပါယ္ရိွပါတယ္။

minOccurs

သူကေတာ့ maxOccurs နဲ ့ေၿပာင္းၿပန္ပါ။ XML Document ထဲမွာ element တစ္ခုဟာ အနည္းဆံုး ဘယ္ႏွယ္ ၾကိမ္ပါ၀င္ႏိုင္လဲဆိုတာကို သတ္မွတ္ေပးပါလိမ့္မယ္။ မရိွသံုးတန္ဖိုး (default value ) က 1 ပါ။

တကယ္လို ့ feature ဆုိတဲ့ element ေလးကို ေၾကညာခ်င္တယ္ဗ်ာ။ အ၀တ္အထည္ အတြက္ေပါ့။ အဲဒီမွာ အနည္းဆံုးအေနနဲ ့ feature ၂ ခု၊ အမ်ားဆံုးအေနနဲ ့ feature ၆ ခုကို ပဲ ရိွေစခ်င္တယ္ဆိုပါစို ့။ ဒါဆို-

<element name="feature" minOccurs="2" maxOccurs="6">


(ကၽြန္ေတာ္တို ့ Database နဲ ့တြဲသံုးတဲ့ အခါ၊ Database မွာ attribute(colum) ရဲ ့ value က allow null (တန္ဖိုး မထည့္လည္း ရတဲ့ column) ဆိုရင္၊ XML တြဲၿပီးအလုပ္လုပ္တဲ့ အခါ၊ minOccurs ကို 0 တန္ဖိုး ေပးလိုက္ ဒါဆုိ အဲဒီ element (tag) ကမပါလည္း ရသြားၿပီး။ ဟုတ္။)

nillable
ဒီဟာေလးကေတာ့ boolean တန္ဖိုး ၿဖစ္တဲ့ true နဲ ့ false တစ္ခုခုကို လက္ခံပါတယ္။ သူက ဘာကိုေၿပာသလဲ ဆိုေတာ့ element တစ္ခုဟာ ဘာမွမပါတာ(nillable)ကို လက္ခံလားမခံလားဆိုတာကိုပဲ။ လက္ခံတယ္ (true) ရင္ element တစ္ခုဟာ EMPTY element ၿဖစ္ႏိုင္ပါတယ္။

<part></part> (၀ါ) <part/> (empty element)


ref

သူကေတာ့ နည္းနည္း ရွဳပ္သလို ရိွတယ္။

ref စြမ္းရည္ၿပစကားလံုး (attribute) ေလးဟာ global ေၾကညာထားတဲ့ elemnt တစ္ခုကို ၿပန္ၿပီး ေတာ့ သံုးခ်င္တဲ့ အခါမွာ အသံုးၿပဳပါတယ္။ ဥမပါ ၾကည့္ရေအာင္ -

<xs:schema>
<xs:element name="full_name" type="xs:string"/>
<xs:element naem="supervisor">
<xs:complexType>
<xs:element ref="full_name"/>
</xs:complexType>
</xs:element>
<xs:element name="employee">
<xs:complexType>
<xs:element ref="full_name"/>
</xs:complexType>
</xs:element>
</xs:schema>


ဒီ ဥပမာေလးမွာ full_name element ေလးတစ္ခုကိုေၾကညာထားပါတယ္။ ပီးေတာ့ ref attribute ကို သံုးၿပီး ေတာ့ supervisor နဲ ့ employee element တို ့မွာ ၿပန္ညြန္းထားပါတယ္။ ဒီေနရာမွာဘာ ကအေရးၾကီးလဲဆို full_name ကို global scope မွာ ေၾကညာရတာပါပဲ။ ref ကို သံုးခ်င္ရင္၊ အသံုးခံရမယ့္ element ဟာ global ၿဖစ္ေစ၊ သံုးမယ့္ element နဲ ့ level တစ္ခုတည္းမွာၿဖစ္ေစရိွရပါမယ္။ အေပၚဥပမာဆိုရင္၊ full_name elemnt ဟာ (အနီေရာင္) ၿပန္ၿပီး reference လုပ္မဲ့ ဟာႏွစ္ခု (အစိမ္းေရာင္) တို ့ရဲ ့ အၿပင္ global scope မွာရိွ ေနတယ္။ ဟုတ္။ ေနာက္သတိၿပဳရမွာတစ္ခ်က္က ref ကို global ေၾကညာမယ့္ element မွာ မသံုးဘူးဆိုတာပါပဲ။ (အေပၚဥပမာဆိုရင္၊ full_name မွာ ref မသံုးထားပါ)

ဒါေၾကာင့္ သေဘားတရားအရ ေအာက္ပါဥပမာမ်ိဳး ကို လုပ္လို ့မရပါဘူး။

<xs:element>
<xs:element name="full_name" type="xs:string"/>
<xs:element ref="full_name" type="xs:string"/>
<xs:schema>


သေဘာေပါက္တယ္ဟုတ္ :D ။

ေလာေလာဆယ္ေတာ့... .
ဆက္ရန္။ (အားေသးဘူးဗ်ာ)။

အာေကလာ(akela)
Dec 04 , 2007 9:23 pm

Wednesday, November 28, 2007

XML Schemas
(XML သဒၵါ (၀ါ) XML အဓိပၸါယ္ သတ္မွတ္မွဳ) - အပိုင္း ( ၂ )

အပိုင္း ( ၁ ) ကို အရင္ဖတ္ၾကည့္ေစခ်င္ပါသည္။

Valid <element>  Attribute


XML Schema မွာ element တစ္ခုကိုေၾကညာမယ္ဆိုရင္ <element> ဆိုတဲ့ tag ကို အသံုးၿပဳရပါတယ္။ ဒီ တက္(ဂ္)ေလးကဘယ္ element ရဲ ့ အစမွာဆိုပါပါတယ္။ ဒီ <element> tag ေလးမွာ attribute လို ့ေခၚတဲ့ ဒီတက္ေလးဟာ နာမည္ဘယ္လိုေခၚေၾကာင္း၊ ဘာေဒတာအမ်ိဳးအစားၿဖစ္ေၾကာင္း၊ စသည္ၿဖင့္ ေၿပာၿပ ေပးမယ့္ စြမ္းရည္ၿပစကားလံုး (attribute) ေလးေတြရိွပါတယ္။ (ေအာက္မွာၾကည့္ပါ)

    - default
- fixed
- id
- maxOccurs
- minOccurs
- name
- nillable
- ref
- substitutionGroup
- type


ဟုတ္ပီလား။ ကၽြန္ေတာ္တို ့ တစ္ခုခ်င္းစီကို ၾကည့္ၾကရေအာင္။ ဒါမွ element ကိုေၾကညာတဲ့အခါ ဘယ္လိုတည္ေဆာက္လို ့ ရတယ္ဆိုတဲ့ structure ကို သိသြားမယ္။

name
ဒီ name ဆိုတဲ့ စြမ္းရည္ၿပ စကားလံုး (attribute) ေလးက XML document ဖိုင္တစ္ခုထဲ မွာရိွတဲ့ element တစ္ခုရဲ ့ နာမည္ကို သတ္မွတ္ေပးပါတယ္။ ဥပမာ - XML Schema မွာ ဒီလို ေရးထားတယ္ဆိုပါဆို ့။

<xs:element name="address"/>


ဒါဘာကိုေၿပာတာလည္းဆိုရင္ ဒီ Schema ကို သံုးမယ့္ XML document ထဲမွာသံုးႏိုင္တဲ့ <address> ဆိုတဲ့ tag ကို ေၾကညာလိုက္တာပါပဲ။ ဒီလို name ကိုတန္ဖိုး သတ္မွတ္ေပးတဲ့ အခါ (ဒီေနရာမွာ address ေပါ့) letter နဲ ့စရပါမယ္။ XML ဆိုတဲ့ စလံုးနဲ ့ ဒါမွမဟုတ္ သူနဲ ့ေပါင္းစပ္ထားတဲ့ စာလံုးနဲ ့ စလို ့မရပါဘူး။ ":" ကို namespace အတြက္ အသံုးၿပဳပီး၊ "_" ကို ေတာ့ သံုးႏိုင္ပါတယ္။
ိုု
defualt
ဒါေလးက string အမ်ိဳးအစား တန္ဖိုးတစ္ခုကို ကၽြန္ေတာ္တို ့့element ရဲ ့ မရိွသံုးတန္ဖိုး (default value) အၿဖစ္ထည့္ေပးပါတယ္။ ဒါေၾကာင့္ တကယ္လို ့ copyright element တစ္ခုကို ဒီလိုေရးခဲ့မယ္ဆိုရင္ -

<element name="copyright" default="Copyright 2008"/>


ဒါဆိုရင္ <copyright> လို ့ေခၚတဲ့ element တစ္ခုကို ဖန္တီးပီးေတာ့ အဲဒီ element မွာဘာတန္ဖိုး (value) မွ မထည့္ခဲ့ရင္ မရိွသံုးတန္ဖိုး (default value) အေနနဲ ့ Copyright 2008 ကိုယူသြားမွာၿဖစ္ပါတယ္။ ဥပမာ -

<copyright></copyright>  


ဒီလိုၿဖစ္ေနခဲ့ရင္ တန္းဖိုးမရိွေတာ့
Copyright 2008 သူရဲ ့တန္ဖိုး(value) အေနနဲ ့ယူသြားမွာၿဖစ္တယ္။ဒါနဲ ့ အတူတူပါပဲ။


<copyright>Copyright 2008</copyright> 


တကယ္လို ့ တန္းဖိုးရိွခဲ့တယ္ဆိုရင္ေတာ့ အဲဒီတန္ဖိုးကိုပဲယူပါတယ္။ မရိွသံုးတန္ဖိုး (default value) ကို မသံုးပါဘူး။

XML document file ကို ေရးတဲ့ သူက အဲဒီ element ကို တန္ဖိုး ထည့္ေပးေပးမေပးေပး မစဥ္းစားဘူး။ element တစ္ခုက တန္ဖိုး(value) တစ္ခုရိွကိုရိွရမယ္။ အဲဒီလို အခ်ိန္မ်ိဳးမွာ ဒီ default ေလးဟာ အလြန္အသံုးက်ပါတယ္။

fixed
ဒီဟာကေတာ့ string အမ်ိဳးအစား တန္ဖိုးတစ္ခုကို element ရဲ ့ တန္ဖိုးအၿဖစ္သတ္မွတ္ေပးလိုက္ပါတယ္။ သေဘာက XML Schema ဖိုင္မွာ ဒီလို ေၾကညာခဲ့မယ္ဆိုရင္ -

<xs:element name="info" fixed="For Information No 001"/>


ဟုတ္ပီ။ ဒါဆိုရင္ XML Document ဖိုင္ထဲမွာ <info> ဆိုတဲ့ tag ရိွမယ္ေပါ့။ အဲဒီ tag ေလးဟာ
For Information No 001 ဆိုတဲ့ တန္ဖိုး(value) ပါကိုပါရမယ္။

<info>For Information No 001</info>


XML Document ဖိုင္ကို ေရးတဲ့ သူကို တန္ဖိုး(value) မေၿပာင္း ေစခ်င္ဘူးဆိုရင္
ဒီ fixed စြမ္းရည္ၿပစကားလံုး (attribute) ကို သံုးပါတယ္။ Schema မွာ ေၾကညာထားတဲ့ တန္ဖိုး (ဒီေနရာမွာ Information No 001) ကို သံုးကိုသံုးရပါတယ္။ XML Document ဖိုင္ကေန ေၿပာင္းလို ့မရပါဘူး။

ဆက္ရန္။

အာေကလာ(akela)
Nov 28 , 2007 1:00 am

Tuesday, October 23, 2007

XML Schemas
(XML သဒၵါ (၀ါ) XML အဓိပၸါယ္ သတ္မွတ္မွဳ) - အပိုင္း ( ၁ )

Document Type Definition (DTD) ဟာ တခ်ိဳ ့ စြမ္းေဆာင္ခ်က္ေတြကို ရယူေပးဖို ့အတြက္ အကန္ ့အသက္ရိွေပမယ့္ XML ရဲ ့သဒၵါ (Schema) ကို ေဖာ္ၿပဖို ့ အတြက္ တကယ္ကို ေကာင္မြန္တဲ့ နည္းလမ္းတစ္ခုပါပဲ။ DTD ဟာ အေမြဆက္ခံၿခင္း (inheritance)၊ နယ္နိမိတ္သတ္မွတ္္ၿခင္း (scoping) တုိ ့ကို ခြင့္မၿပဳပါ ဘူး။ ဒါအၿပင္ ေဒတာ အမ်ိဳးအစားသတ္မွတ္ၿခင္း (datatyping) ကိုလည္း DTD နဲ ့လုပ္လို ့မရပါ။ DTD က ခြင့္မၿပဳေပမယ့္ ဒီ ကိစၥေတြဟာ XML ဒီဇိုင္းအတြက္ အသံုး၀င္တဲ့ အရာေတြပါ။ ဒါေၾကာင္း XML Schema မွာေတာ့ ဒါေတြကို လုပ္ေဆာင္ႏိုင္ေအာင္ ထည့္သြင္း တည္ေဆာက္္ခဲ့ပါတယ္။

XML Schemas ဆိုတာ သူတို ့ကိုယ္ႏိွက္က well-formed XML ေတြပါပဲ။ ဒါဟာ XML Schemas ရဲ ့ အေတာ္ကို ၾကီးမားတဲ့ အားသာခ်က္တစ္ခုပါပဲ။
ရိုးရွင္းတဲ့ XML Schema တစ္ခုကို ၾကည့္ရေအာင္။

<?xml version=”1.0” encoding=”utf-8” ?>
<xs:schema xmlns:xs = “http://www.w3.org/201/XMLSchema”>
<xs:element name=”Occupation” type=”xs:string”/>
</xs:schema>


ဒါဟာ အလြန္ရိုးရွင္းတဲ့ schema တစ္ခုပါပဲ။ ဒီ schema က Occupation ဆိုတဲ့ element တစ္ခုကို သတ္မွတ္ေပးပါတယ္။ အဲဒီ element ရဲ ့ ေဒတာ အမ်ိဳးအစားက string ပါ။


<?xml version=”1.0” encoding=”utf-8” ?>


ပိုၿပီး အေသးစိတ္ၾကည့္ရေအာင္။ ဒါက XML ေၾကညာတာပါပဲ။ ဒီဟာေလးဟာ ဘယ္ XML ဖိုင္ တိုင္းရဲ ့ အစမွာမဆို ပါေလ့ရိွပါတယ္။ သူက အသံုးၿပဳမယ့္ application ကို XML ဖိုင္ၿဖစ္ေၾကာင္း 1.0 XML Recommendation ေပၚမွာ အေၿခၿပဳထားေၾကာင္း၊ UTF-8 character encoding ကို သံုးထားေၾကာင္း စသည္တိုကို သိေစပါတယ္။ (XML schema ဟာ XML ပဲဆိုတာကို မေမ့ပါနဲ ့ဦး။) ဒီလို တူတဲ့ အတြက္ parser တစ္ခုတည္းကို XML document ကိုေရာ schema အတြက္ကိုေရာ အသံုးၿပဳႏိုင္ပါတယ္။

ထပ္ၾကည့္မယ္ဆိုရင္ ဒီလို ေတြ ့ပါလိမ့္မယ္။

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">


ဒီအေၾကာင္းေလးက xs (အနီေရာင္ႏွင့္ၿပထားသည္။) ဆိုတဲ့ Namespace ကို သတ္မွတ္ေပးပါတယ္။ ဒီမွာသံုးထားတဲ့ xs ဆိုတာ http://www.w3.org/2001/XMLSchema ရဲ ့ xs ပါတဲ့ ။

ဒါကလဲ အေရးၾကီးပါတယ္။ သူက ေနာက္ပိုင္းကၽြန္ေတာ္တို ေၿပာဆိုေဆြးရမယ့္ နယ္နိမိတ္သတ္မွတ္္ၿခင္း (scoping) နဲ ့ ဗားရွင္းသတ္မွတ္ၿခင္း (versioning) တိုကို ၿဖစ္ေစပါတယ္။

<xs:element name="programmer" type="xs:string"/>


ဒါကေတာ့ ကၽြန္ေတာ္တို ့ရဲ ့ Schema ကုိသံုးၿပီး element တစ္ခုကို ေၾကညာလိုက္တာပါပဲ။
ဒီေနရာက xs:element ဆိုတာမွာ xs က အေပၚက ကၽြန္ေတာ္တို ့ေၾကညာခဲ့တဲ Namespace ကိုယူသံုးလိုက္တာပါ။ သူက programmer ဆိုတဲ့ element တစ္ခုကို ေၾကညာေၾကာင္း၊ အဲဒီ element ရဲ ့ ေဒတာအမ်ိဳးအစား က string ၿဖစ္ေၾကာင္းတို ့ကို ေၿပာၿပပါတယ္။ ဒီေနရာမွာ type ကေလးက XML Schema မွာ အေရးၾကီးတဲ့ attribute ေလးပါပဲ။ သူက ေဒတာအမ်ိဳးအစားသတ္မွတ္ၿခင္း (datatyping) ကို လုပ္ေပးတယ္ေလ။

ဆက္ရန္။

ရည္ညြန္း။ ။ Using XML by David Gulbransen, et al.

အာေကလာ(akela)
Oct 23 , 2007 7:51 pm


Tuesday, August 21, 2007

Scope in JavaScript (part 4)
(JavaScript ????????? ????????? ????????) - ??????? ?

(??????? ? ? ? ????? ? ??? ????????????????????????)


.bind() ???????? ? ???


????????????? apply() ? call() ??? ? ?? ? ???????????????????????????? ?????????? .bind() ?????????? ????????? ????? ?????????? function ?? ? ??????(reference) ???? ???? (return) ????????????????????????????? ?????????????? ???????????? ??? ???????????????? ??????? ????????????? ?????????????????


<script type="text/javascript">
var _president = {
_answer: "I am the president"
};
var _secaretary = {
_answer: "I am the secaretary of president"
};

function aboutMe(greetingWord) {
return greetingWord + " " +this._answer;
}

Function.prototype.bind = function(obj) {
var method = this,
temp = function() {
return method.apply(obj, arguments);
};

return temp;
}
var whatTheysays;

whatTheysays = aboutMe.bind (_president, ["Hello My Country,"]); // "Hello My Country, I am the president"
alert(whatTheysays);

whatTheysays = aboutMe.bind (_secretary, ["Hello My Officer,"]); // "Hello My Officer, I am the secretary of president"
alert(whatTheysays);
</script>


???????????? ? bind() ??? Function object ?? ? prototype propety ??? ???????? ?????????????? ???? ???????????? ? ?????????? ? ?????? (method) ??????? ??? ?????????????????????? aboutMe.bind(_president) ????????? ?????? Javascript ? bind() ??????????? execution context ???????? ???????????? ???????? this ??? ???? aboutMe ??? ??????????????? ????? ??? prarmeter ??????? obj ??? _president ?????????????????? ??????????????? :D?

???? ????? ? ???????? method ??? ?????? ??????????? method ????? this ??? ???????? ???????? ???? this ? aboutMe() ??? ????? ???????? ?????????? function ??? ?????????????????? ???????????????????? method ??? scope chain ??????? ???? ???????????? ????????? this ??? ??????? ? ???????? ??????? function ???? ??? ????????? ????????? this ? ???? ????? ???????? (local context) ?????????????? aboutMe() ??????????????? ??????? function ?? ????????? ???????? ??????????????

??????????????????? ?? ????????? temp ? closure (????????????????????? ????????????????) ????? bind() ??? ??????? ? ???? (return ) ?????????? aboutMe ??? _president object ???????? ?????????????????? ?????? ????????? ????? execution context ??? ??????????? ? ??????? ???????????? ???????????????????

??? ???????????? ? event handler ????? ????????????? ??????????????????? :D ? ????? ???????? ????? event handler ??? ????? ????????????????


<script type="text/javascript">

var the_button;
function aboutMe(answer)
{
this._answer = answer;
this.tell = function() {
alert(this._answer);
}
}
Function.prototype.bind = function(obj) {
//alert(this + 'this in bind');
//alert(arguments[0].the_answer);
var method = this,
temp = function() {
return method.apply(obj, arguments);
};

return temp;
}

function addhandler()
{
var _president = new aboutMe(“I am the president.”),
var the_button = document.getElementById('thebutton');
the_button.onclick = _ president.tell();
//alert(this + ' this in addhandler');
the_button.onclick = _president.tell.bind(_president);
}
</script>

……
……

<BODY onload="javascript:addhandler();" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#FF0000" VLINK="#800000" ALINK="#FF00FF" BACKGROUND="?">
<button ID="theButton">Click Me</button>
</BODY>
</HTML>


??? html ??? body ?? ? onload ??? addhandler() ??? ???? ?????????????????????????

????? ?????????????????????????.?

?????????????????? ????????


??????(akela)
Augest21, 2007 12:11 pm

Saturday, August 18, 2007

Scope in JavaScript (part 3)
(JavaScript မွာရိွေသာ နယ္နိမိတ္ သေဘာတရား) - အပိုင္း ၃

(အပိုင္း ၁ နဲ ့ ၂ ကို အရင္ဖတ္ၾကည့္ေစခ်င္ပါသည္။)

Context မ်ားကို .apply() နဲ ့ .call() မတ္သတ္(method) မ်ားသံုးၿပီး ကိုင္တြယ္ၿခင္း

တကယ္ေတာ့ ကၽြန္ေတာ္တို ့လို ခ်င္ခဲ့တာဟာ object ရဲ ့ မတ္သတ္(method) ကို events(အဗင့္) လိုမ်ိဳး၊ setTimeout() လို မ်ိဳး ကိုကိုင္တြယ္ဖို ့ သံုးတဲ့အခါက်ရင္ အဲဒီ method(မတ္သတ္)ကို သူရဲ ့ native context(မူလ ကြန္းတက္) ထဲမွာပဲ အလုပ္လုပ္(run) ေစခ်င္တာပါ။ လူသိနည္းတယ္လို ့ ဆိုႏိုင္တဲ့ မတ္သတ္ ၂ ခုရိွပါတယ္။ apply() နဲ ့ call() ပါ။ ဒီ မတ္သတ္ႏွစ္ခုကို သံုးၿခင္းအားၿဖင့္ ကၽြန္ေတာ္ တို ့ ဟာ this ရဲ ့ default value အေပၚကို လႊမ္းၿပီး(override) ေရးႏိုင္ပါတယ္။

<script type="text/javascript">
var _president = {
_answer: "I am the president"
};
var _secretary = {
_answer: "I am the secretary of president"
};
function aboutMe(greetingWord)
{

return greetingWord + " " +this._answer;
}
var whatTheysays;
whatTheysays = aboutMe.call(_president, "Hello My Country,");
alert(whatTheysays);
whatTheysays = aboutMe.call(_secretary, "Hello My Officer,");
alert(whatTheysays);
</script>


ဘယ္လိုလဲ :D ။ အလုပ္လုပ္တယ္ဟုတ္။ :P။

ဒီဥပမာေလးမွာဆိုရင္ ကၽြန္ေတာ္တို ့ president object ရယ္ secretary object ရယ္ဆိုၿပီး object ႏွစ္ခု ဖန္တီးလိုက္ပါတယ္။ သူတို တစ္ခုစီမွာ _answer ဆိုတဲ့ property ကို နာမည္ (name) အတူတူေပးလိုက္ပါတယ္။ ၿပီးေတာ့ aboutMe() ဆိုတဲ့ function တစ္ခုကိုဖန္တီး လုိက္ပါတယ္။အဲဒီ function က parameter တစ္ခုကိုလက္ခံပါတယ္။ greetingWord ပါ။ အဲဒီ function ထဲမွာ greetingWord ရယ္၊ သူဘယ္သူလဲဆိုတာကိုေၿပာတဲ့ စာအစဥ္အတန္း(string) (this._answre) ရယ္ကိုေပါင္းၿပီးၿပပါတယ္။ သတိထားမိလား.. this က call() ကပိုလိုက္တဲ့ object ေပၚမူတည္ၿပီးေၿပာင္းလဲသြားပါတယ္။ president ဆိုရင္ this.answer ဟာ _president ရဲ ့ _answer ၿဖစ္သြားၿပီး၊ _secretary ဆိုရင္ _secretary ရဲ ့ _answer ၿဖစ္သြားပါတယ္။ ဟုတ္။ ဒါဘာေၾကာင့္လဲ ဆိုေတာ့ call() မတ္သတ္(method) ေၾကာင့္ၿဖစ္ပါတယ္။ ပံုမွန္သာ aboutMe() ကို ေခၚတယ္ဆိုရင္ အားလံုးဟာ undified ပါပဲ။ ဘာလို ့ ဆိုေတာ့ global object ၿဖစ္တဲ့ window မွာ _answer ဆိုတဲ့ property မရိွပါဘူး။ ( ကၽြန္ေတာ္တို ့ က window object မွာ ကိုယ္ဖသာ ဒီ _answer ကို မဖန္တီးထားဘူးခဲ့ရင္ မရိွဘူးလုိ ့ ေၿပာတာပါ။) ဒီ call() မတ္သတ္ေလးက ကၽြန္ေတာ္တို ့လိုခ်င္ေနတာနဲ ့ အားလံုးကြက္တိပဲမဟုတ္လား။ :D ။

apply() က call() နဲ ့တူူတူပါပဲ။ အားလံုး တပံုစံပဲ အလုပ္လုပ္ပါတယ္။ တစ္ခုပဲကြဲတယ္။ဘာလဲဆိုေတာ့ apply() က arguments (parameters) ေတြကို array အေနနဲ ့လက္ခံပါတယ္။ ဒီ apply() မတ္သတ္က function တစ္ခုဟာ parameters ေတြကို ဘယ္ႏွစ္ခုလက္ ခံမယ္ဆိုတာကို ေသခ်ာမသိတဲ့ အခါမ်ိဳးမွာ အလြန္အသံုး၀င္ပါတယ္။

<script type="text/javascript">
.........
.........
whatTheysays = aboutMe.apply (_president, ["Hello My Country,"]);
alert(whatTheysays);

whatTheysays = aboutMe.apply (_secretary, ["Hello My Officer,"]);
alert(whatTheysays);
</script>


apply() နဲ ့ call() တိုဟာ သူဟာနဲ ့သူ အလြန္ အသံုး၀င္ပါတယ္။ ဒါေပမယ္ ဒီ ႏွစ္ခုဟာ တကယ္ေတာ့ ကၽြန္ေတာ္တို ့လိုခ်င္တာရဲ ့ လမ္းတ ၀တ္မွာပဲရိွေနပါေသးတယ္။ event handler အေနနဲ ့သံုးမယ္ဆိုရင္ေလ။ ဒီကုဒ္ေလးေတြကို ၾကည့္လိုက္ပါဦး။

function addhandler() 
{
var _president = new aboutMe(“I am the president.”),
var the_button = document.getElementById('thebutton');
the_button.onclick = _ president.tell.call(_president);
}


စမ္းၾကည့္ၿပီးၿပီ ဟုတ္။ ၿပသာနာက call() ေရာ apply() က ခ်က္ၿခင္း အလုပ္လုပ္(execute) လုပ္တာပါပဲ။ event handler အေနလုပ္တဲ့ အခါ event ကို ရိုတ္ထုတ္ (fire) လိုက္တဲ့ အခ်ိန္မွ မတ္သတ္(method) ကို အလုပ္လုပ္(Run) တာမဟုတ္ပဲ ခ်က္ၿခင္းအလုပ္လုပ္ တာက ၿပသာနာပါ။ ဒီအတြက္ ေနာက္ထပ္လူ သိနည္းတဲ့ function တစ္ခုရိွေနပါတယ္။ အလြန္အသံုး၀င္တဲ့ function ပါ။

ဆက္ရန္။

အာေကလာ(akela)
Augest 18, 2007 11:21 pm
Scope in JavaScript (part 2)
(JavaScript မွာရိွေသာ နယ္နိမိတ္ သေဘာတရား) - အပိုင္း ၂


(အပိုင္း ၁ ကို အရင္ဖတ္ၾကည့္ေစခ်င္ပါသည္။)

တကယ္လို ့ event handler က Inline ဖန္တီးထားတယ္ဆိုရင္၊ this က အားလံုးနဲ ့ဆိုင္တဲ ့ window object ကို ညြန္းပါတယ္။

<script type="text/javascript">
function click_handler()
{
alert(this); // alerts the window object
}
</script>
…..
<button id='thebutton' onclick='click_handler()'>Click me!</button>


တကယ္လို ့ မ်ား event handler ကုိ javaScript ကေနတဆင့္ ဖန္တီးထားတယ္ဆိုရင္၊ this က event ကို ရိုက္ထုတ္လိုက္တဲ ့ DOM (Document Object Model) ကို ညြန္းပါလိမ့္မယ္။


<script type="text/javascript">
function click_handler()
{
alert(this); // alerts the button DOM node
}
function addhandler() {
document.getElementById('thebutton').onclick = click_handler;
}
window.onload = addhandler;
</script>
...
<button id='thebutton'>Click me!</button>


scope က ဘယ္လို ့ ရွဳပ္ေထြးတာလဲ..

ကဲ၊ ကၽြန္ေတာ္တို ့ ေနာက္ဆံုး ဥပမာေလးကို ပဲ ထပ္ၿပီးၿပင္ၾကမယ္။ button ကို ႏိွပ္လိုက္တဲ့အခါတုိင္း click_handler ကို အလုပ္လုပ္ခိုင္း မဲ့ အစား aboutMe ဆိုတဲ့ method ကို ေမးခြန္းေမးပါမယ္။ ကုဒ္ေတြက ရိုးရွင္းပါတယ္။ ေအာက္မွာၾကည့္ပါ။



<script type = “text/javascript”>
function aboutMe(answer)
{
this._answer = answer;
this.tell = function() {
alert(this._answer);
}
}
function addhandler()
{
var _president = new aboutMe(“I am the president.”),
var the_button = document.getElementById('thebutton');
the_button.onclick = _ president.tell();
}
window.onload = addhandler;
</script>


ဟုတ္တယ္ဟုတ္ :D။ ရွင္းရွင္းေလးပဲမလား။ ဘာမ်ားရွဳပ္လို ့လဲဗ်ာ။ button ကို ႏိွပ္လိုက္တဲ့အခါ _president.tell() ကို အလုပ္လုပ္ေစပါတယ္။ ဒီေတာ့ I am the president ဆိုတဲ့ စာအစဥ္အတန္း(string) ကို ရိုက္ထုတ္ၿပမယ္ေလ။ ဟုတ္.. ဘာမွမရွဳပ္ဘူး။ ဒါေပမယ့္ web browser က တကယ္တမ္းဘာရိုက္ထုတ္ၿပလဲ။ စမ္းၾကည့္ေစခ်င္ပါသည္။

စမ္းၿပီးၿပီ..ဟုတ္။ :) ၾကည့္... ဘာမ်ားအမွားလုပ္ခဲ့ပါလိမ့္။ :|

ၿပသာနာကေတာ့..။ ကၽြန္ေတာ္တို ့ tell() ကို button ရဲ ့ onclick event ကို ကိုင္တြယ္ဖို ့ reference အၿဖစ္ပို ့ ေပးခဲ ့ ပါတယ္။ (the_button.onclick = _president.tell();)။ အဲဒီအခါ tell() ဟာ ( event handler အၿဖစ္ အလုပ္လုပ္ေနတဲ့ အတြက္) မတူကြဲၿပားတဲ့ execution context တစ္ခုထဲမွာ အလုပ္လုပ္(Run) ပါတယ္။ လြယ္လြယ္ ေၿပာရရင္ေတာ့ tell() ထဲမွာရိွတဲ့ this keyword က event ကို ရိုက္ထုတ္လိုက္တဲ့ DOM (Document Object Model) (ဒီေနရာမွာေတာ့ button ေပါ့) ကို ညြန္းပါတယ္။ သူဟာ aboutMe ရဲ ့ object မဟုတ္ေတာ့ပါဘူး။ DOM Element ၿဖစ္တဲ့ button မွာ _answer ဆိုတဲ့ property မရိွပါဘူး။ ဒါေၾကာင့္ browser က “I am the president” အစား underfined ကို ရိုတ္ထုတ္ၿပပါတယ္။

ဒီလို ၿဖစ္ရပ္မ်ိဳးက ကၽြန္ေတာ္တို ့ program ေတြမွာ မေမ်ာ္လင့္ ပဲၿဖစ္တက္ပါတယ္။ ေနာက္ၿပီး အမွားကို ရွာဖို ့ တခါတေလ မွာ အေတာ္ခက္ခဲ ပါတယ္။ ဥပမာ - ကၽြန္ေတာ္တုိ ဖန္တီးထားတဲ့ object (ဒီေနရာမွာ aboutMe ေပါ့) ရဲ ့ property နာမည္ (name) က event ကို ရိုတ္ထုတ္လိုက္တဲ့ DOM ရဲ ့ property နာမည္ (name) နဲ ့ သြားတူေနခဲ့မယ္ဆိုရင္ လြယ္လြယ္ အေၿဖရွာဖို ့ မၿဖစ္ႏိုင္ေတာ့ပါဘူး။

ဆက္ရန္။

အာေကလာ(akela)
August 17,2007 3:40 pm

မွတ္ခ်က္။ ။ပိုစ္တစ္ခုနဲ ့တစ္ခုၾကား အခ်ိန္ကြာဟလြန္းေနပါတယ္။ ေနာက္ဆို မကြာေအာင္ေရးသြားပါမယ္။

Saturday, August 4, 2007

Scope in JavaScript (part 1)
(JavaScript မွာရိွေသာ နယ္နိမိတ္ သေဘာတရား) - အပိုင္း ၁

Scope ဆိုတာ JavaScript programming ဘာသာရပ္မွာမွာ အေၿခခံက်တဲ့ အၿမင္ (aspects) တစ္ခုပဲၿဖစ္ပါတယ္။ တစ္ခါတေလ ရွဳပ္ေထြးတဲ့ ပရိုဂရမ္ေတြကို ေရးတဲ့အခါ ဒုကၡအေရာက္ဆံုး အရာလည္းၿဖစ္ႏိုင္ပါတယ္။

JavaScript ပရိုဂရမ္တစ္ပုဒ္ရဲ ့ဘယ္အစိတ္အပိုင္းမဆိုဟာ Execution Context တစ္ခု ဒါမွမဟုတ္ အၿခား Execution Context တစ္ခုထဲ မွာ လုပ္ကိုင္ရပါတယ္။ Execution Context ေတြကို ကၽြန္ေတာ္တို ့ ကုဒ္ေတြပါ၀င္တဲ့ အိမ္နီးနားခ်င္းေတြလို ့ ယူဆႏိုင္ပါတယ္။ ကုဒ္တစ္ေၾကာင္း တိုင္းဟာ သူဘယ္ကေနလာတယ္။ သူ ့ရဲ ့အိမ္နီးနားခ်င္းေတြက ဘယ္သူေတြပါ။ဒါေတြကို ေၿပာၿပပါလိမ့္မယ္။ ဒီ Execution Context ေတြဟာ အေရးၾကီးပါတယ္။သူတုိ ့ကို ပြင္ေန ့တဲ ့၊ ဖြင့္ ထားတဲ့ အပိုင္း အခြဲငယ္ေလးေတြလို ့ယူဆတာထက္၊ အစည္းအတား ရိွတဲ့ ဆက္ သြယ္မွဳ ့နယ္ပယ္ေလးေတြ (gated communities) လို ့ယူဆရင္ပိုေကာင္းမယ္လို ့ ထင္ပါတယ္။

ဒီအစည္းအတား နယ္ပယ္ေတြကို ကၽြန္ေတာ္တို ့က scope လို ့ ေၿပာဆိုၾကတယ္။ သူတို ့ဟာ ဒီ Execution Context တစ္ခုစီရဲ ့ ဥပေဒသ သေဘာသဘာ၀ (Ruls and Laws) ေတြ ရဲ ့သက္ေရာက္မွဳ ့ကို သတ္မွတ္ေပးမွာၿဖစ္လို ့ အေရးၾကီပါတယ္။ ဒါကို Execution Context ရဲ ့ Scope Change လုိ ့ေခၚ ပါတယ္။ Execution Context တစ္ခုထဲမွာ ရိွတဲ့ ကုဒ္ေတြဟာ အဲဒီ Execution Context ရဲ ့ Scope Change မွာ ပါ၀င္တဲ့ စာရင္း (list) အတိုင္းပဲ Variable ေတြကို အသံုးၿပဳလို ့ရပါတယ္။

method တစ္ခု ကို အလုပ္လုပ္ခိုင္း(Evaluate) ၿပီဆိုရင္ Execution context အသစ္တစ္ခုကို ဖန္တီးတည္ေဆာက္ပါတယ္။ ၿပီးေတာ့ သူရဲ ့ scope ကို အဲဒီ method ကိုေက်ညာထားတဲ့ ေနရာမွာရိွတဲ့ scope chain ထဲမွာ သြားၿပီးေပါင္းပါတယ္။ Execution Context တစ္ခုရဲ ့scope ကိုၾကည့္တဲ့အခါ အတြင္းမွ အၿပင္ကို (locally then globally)တဆင့္ၿခင္းတက္ၿပီးၾကည့္ပါတယ္။ ဒါကဘာကိုေၿပာတာလဲဆိုေတာ့ အတြင္း (local) က အၿပင္(global ) ထက္ပိုၿပီးဦးစားေပးခံရပါတယ္။ ဥပမာ - နာမည္တူေနတဲ့ variable ႏွစ္ခု၊ တစ္ခုု က အတြင္း (local) ၊ အၿခားတစ္ ခုကအၿပင္ (Global) ဆိုၿဖစ္ခဲ့တယ္ဆိုပါေတ့ာ။ ဘယ္ variable ကို ယူမလဲဆိုရင္ local variable ကို ပဲ ယူပါတယ္။

this? ဘာလဲ this ဆိုတာ။

Execution context တိုင္းက scope chain ကို ဖန္တီးတည္ေဆာက္ရံုုမက this ဆိုတဲ့ keyword ေလးတစ္ခုလည္းရိွပါေသးတယ္။ ပံုမွန္အား ၿဖင့္ဆိုရင္ေတာ့၊ ဒီ this ေလးက identity function ေလးပါ။ အိမ္နီးခ်င္း context ေတြကို သူ တို့ ကိုသူတို ့ဘယ္လို ၿပန္ၿပီး ညြန္လို ့ရႏိုင္မလဲ ဆိုတာ က္ုိလည္း ေၿပာၿပပါတယ္။ ဒါေပမယ့္. ကၽြန္ေတာ္တို ့ဟာ ဒီအခ်က္ေပၚမွာ အၿမဲတမ္း မွီတည္ ေနလို ့ မရပါ ဘူး။ အၿခားအိမ္နီးခ်င္း context ေတြထဲကို ဘယ္လို ေရာက္သြားတာလဲအေပၚမူတည္ၿပီး this ရဲ ့အဓိပါယ္က လံုး၀ကြဲၿပားသြားပါတယ္။ အၿခားအိမ္နီးခ်င္း context ေတြထဲ ဘယ္လို ေရာက္သလဲဆိုတာဟာ this ေလးက လက္ရိွ ဘာကို ညြန္းေနလဲလို ့ဆိုလိုတာပါ။

Object-oriented programming မွာ လက္ရိွ ရိွေနတဲ့ လုပ္ကိုင္ေနတဲ့ object ကို ၿပန္လည္ညြန္းၿပီးသံုး ႏိုင္ဖို ့အတြက္ နည္းလမ္းလို ပါတယ္။ this က ဒီလို အပ္ခ်က္ကို ၿဖည့္စည္းေပးပါတယ္။ object ေတြကို သူတို ့ကို သူတို ့ၿပန္ညြန္းဖို ့နဲ ့၊ သူတို ပိုင္ဆိုင္တဲ့ properties ေတြ၊ methods ေတြကို ၿပန္သံုးဖို ့ this က လုပ္ေပးပါတယ္။ ေအာက္ပါ အခ်က္ ၄ ခ်က္က စဥ္းစားဖိုေကာင္းပါတယ္။

၁။ Calling an Object’s Method (Object တစ္ခုရဲ ့ method ကို ေခၚၿခင္း)

<script type = “ text/javascript”>
var _phoLay = {
_answer: “I’m Pho
Tay.”,
tell:function() {
return this._answer;
}
};
var _whoAmI = _phoLay.tell ();
</script>


ဒီကုဒ္ေလးေတြဟာ _whoAmI ဆိုတဲ့ object တစ္ခုကို ဖန္တီးပါတယ္။ ၿပီးေတာ့ သူရဲ ့ property ၿဖစ္တဲ့ _answer ဆိုတဲ ့variable ထဲကို “I’m Pho Tay.” ဆိုတဲ့ စာအစဥ္အတန္း (String) တစ္ခု ထည္ပါတယ္။ေနာက္ tell ဆိုတဲ့ method တစ္ခုကိုေၾကညာပါတယ္။ ဒီ method ကို အလုပ္လုပ္ခိုင္းတဲအခါ JavaScript က execution context တစ္ခု တည္ေဆာက္ပါတယ္။ this ကိုေတာ့ ေနာက္ဆံုးး “.” (dot) ရဲ ့ ေရွမွာ ရိွတဲ့ ဘယ္ object ကိုမဆို ညြန္းခုိင္းပါတယ္။ ဒီမွာ ေတာ့ _whoAmI ေပါ့။ (_whoAmI.tell() ေလ။) tell() ဆိုတဲ့ method ေလးထဲ မွာေတာ့ this က ေနတဆင့္ သူပိုင္တဲ့ property ကိုၾကည္ပါတယ္။ ၿပီးေတာ့ အဲဒီ property ၊ this._answer ထဲက တန္ဖိုးကို return ၿပန္ပါတယ္။ (I’m Pho Tay.)။

၂။ Constructor (Constructor ကို ေခၚၿခင္း)


အေပၚကလို ပါပဲ။ new ဆိုတဲ့ keyword ကို သံုးၿပီး function တစ္ခုကို constructor အၿဖစ္သံုးတဲ့ အခါ၊ this ကို ဖန္တီးခံရတဲ့ object ကို ညြန္းတဲ့ အရာအေနနဲ ့ သံုးႏိုင္ပါတယ္။


<script type = “text/javascript”>
function aboutMe(answer) {
this._answer = answer;
this.tell = function() {
return this._answer;
}
}
var _phoLay = new aboutMe(“I’m pho lay.”);
var _whoAmI = _phoLay.tell();
</script>


ဒီကုဒ္မွာ ေတာ့ phoLay object ကုုိ ကိုယ္တုိင္တိုက္ရိုက္ိ explicitly ဖန္တီးမယ့္အစား ၊ aboutMe ဆိုတဲ့ function တစ္ခုကို ေရးလိုက္ပါတယ္။ ၿပီးမွ phoLay ကို aboutMe ရဲ ့ instance အၿဖစ္ new keyword ကေနတဆင့္ ရယူပါတယ္။ new aboutMe() ဆိုတဲ့ method ေလးကို အလုပ္လုပ္(execute)လုပ္ခိုင္းတဲ့အခါ ၊ ေနာက္ကြယ္မွာ လံုး၀ကြဲၿပာၿခားနားတဲ့ object တစ္ခုကို ဖန္တီးတည္ေဆာက္ပါ တယ္။ aboutMe() ကိုေခၚတဲ့အခါ ၊ this ကို အဲဒီ ဖန္တီးထားတဲ့ object ကို ညြန္းေစပါတယ္။ this က တၿခား variable ေတြလို scope chain ကေန ရယူတာမဟုတ္ပါ။ ဒါေပမယ္ သူက context by context ေပၚမူတည္ၿပီး ညြန္းတာေၿပာင္းလည္းပါတယ္။ context တစ္ခုကေန တစ္ခုကိုေၿပာင္းရင္ reset လုပ္ခံရပါတယ္။

၃။ Function Call (Function ေခၚၿခင္း)


တကယ္လို ့ ပံုမွန္ေရးသလို ပဲေပါ။ ေန ့စဥ္ေရးေလ့ရိွတဲ့ function လိုမ်ိဳး ရွဳပ္ေထြးတဲ့ object ေတြမပါခဲ့ဘူးဆိုရင္ this က ဘာကိုဆိုလိုသလဲ။


<script type = “text/javascript”>
function testing_this() {
return this;
}
var whatIsThis = testing_this();
</script>

ဒီမွာ this က ဘာကိုညြန္းပါသလဲ။ new keyword ကိုသံုးၿပီး context တစ္ခုလည္းမလုပ္ထားဘူး။ သူဘာကိုညြန္းပါသလဲ။ ဒီေနရာမွာ this ဟာ Global အက်ဆံုးအရာကို ညြန္းပါတယ္။ ဥပမာ - web page ေတြမွာဆိုရင္ global အက်ဆံုးက window object ပါ။ this က window object ကို ညြန္းပါလိမ့္မယ္။

၄။ Event Handler (Event မ်ားကို ကိုင္တြယ္ၿခင္း)


နည္းနည္းပိုၿပီးရွဳပ္ေထြးတဲ့ function calling ေတြမွာ၊ ဥပမာ- onclick event ကို ကိုင္တြယ္ဖို ့ function တစ္ခုသံုးမယ္ဆိုပါစို ့။ event ၿဖစ္ပြားတဲ့ အခါ this ကဘာ ကို ညြန္းမလဲ။ ဒီအတြက္ ရိုးရွင္းတဲ့အေၿဖတစ္ခုကို မေပးႏိုင္ခဲ့ၾကပါဘူး။

ဆက္ရန္။

အာေကလာ(akela)
August 4,2007 1:50 am

မွတ္ခ်က္။ ။ဖတ္မိေသာ article တစ္ခုကို ၾကိဳက္မိ၍ တက္နိုင္သမွ် ၿမန္မာလို ၿပန္လည္ေရးသားထာပါတယ္။
ေပ်ာ္ရြင္ခ်မ္းေၿမ ့ၾကပါေစ။