Tải bản đầy đủ - 0 (trang)
Chapter 8. Debugging and Deploying a Gadget

Chapter 8. Debugging and Deploying a Gadget

Tải bản đầy đủ - 0trang

DebuggingaGadget

Thedebuggingprocessnormallyinvolvestracingthecode,and

watchingvaluesandthestateofthegadgetduringthegadget's

execution.Themostcommonscenariosare

Findingthevalueofavariableataparticularpointintime

Evaluatinganexpression

Branchingtheexecutionofthecodebasedonaparticular

conditionorvalue

Gadgetdevelopmentisbasedonthescriptinglanguage,and

becausetheruntimeenvironmentofthegadgetistheSidebar

itself,debuggingcapabilitiesarequitelimited.Herearesomeof

thewaystoefficientlydebugagadget:

UseadditionalscriptssuchasWindowsScriptHost



(WScript)orVisualBasicScript(VBScript)toassistyou,the

developer,intracingthegadget'sbehavior.

UseadditionalmethodsintheDOMtoassistinvalidating

gadgetbehaviorbydisplayingmessagesatspecificpointsin

thegadget'sexecution.

Usedebuggingtoolsthatenableyoutoinspectthegadget

state.

Thefollowingsectionslookintoeachofthesemethods.



DebuggingUsingWScriptandVBScript

Theeasiestwaytodebugagadgetistouseamessageboxto

displaythevalueofavariableoranexpressioninthegadgetat

differentstepsofdebugging.

TheWindowsScriptHost(WScript)ActiveXobjectandVisual

BasicScript(VBScript)bothprovidemethodstocreatea

messageboxinthegadget.WindowsScriptHostgivespop-up

functionality,whereasVBScripthasabuilt-inMsgBoxfunction.



JavaScriptAlertandConfirm

JavaScripthasasimilaralertandconfirmfunction,butboth

ofthesemodaldialogsaresuppressedforVistagadgets.Using

pop-updialogsgoesagainsttheWindowsVistaUserExperience

guidelinesforSidebargadgets.MoreinformationonWindows

VistaUserExperienceGuidelinescanbefoundat

http://msdn2.microsoft.com/en-us/library/aa511258.aspx.

Notethatmessageboxfunctionalityisusedherejustforthe

debuggingpurpose.Useofmessageboxasafeatureofthe

Vistagadgetisstillnotagooddesign.



DebuggingUsingWScript

TheWindowsScriptHostisanActiveXobjectprovidedbythe

Windowsoperatingsystemtoperformadvancedsystem-level



functionalities.Itcanbeusedinagadgettogenerateadialog

boxfordebuggingpurposes.

TousetheWindowsScriptHostpop-updialog,liketheone

showninFigure8.2,youneedtocreateaninstanceofashell,

usinganActiveXobjectcontrol:

90



varWshShell=newActiveXObject("WScript.Shell");



Andcallthepopupfunction

90



vari=WshShell.Popup("HelloGadgetWorld",7,

"InnovateWithGadgets!",32);



Figure8.2.WindowsScriptHostcanbeusedtocreatea

messageboxsimilartoaJavaScriptalert.



YoucanwrapthefunctionalityprovidedbytheWindowsScript



HostintoaJavaScriptfunctionthattakesamessagetodisplay

asaparameter.Hereissamplecodetocreateapop-up

messagebox,usingtheWindowsScriptHostActiveXobject

wrappedinsideaJavaScriptfunctionMessageJS:

90



functionMessageJS(prompt)

{

varWshShell=newActiveXObject("WScript.Shell");

varreturnValue=WshShell.Popup(prompt,7,"InnovateWithGadg

}



ImagineyouhaveaddedafeedintheMyBlogGadgetbutyou

don'tseeanythinginthegadgetwindow.Howwillyoudebugto

findoutwhat'shappening?Followthesesteps:

1. AddtheMessageJSfunctioninthegadget.jsfile.

2. IntheParseFeedfunction,addthefollowingbeforethecode

whereyouputthetitlestothearray:



90



MessageJS(i+":"+rssTitle);

Titles[i]=rssTitle

Descriptions[i]=rssSummary;

PublishedList[i]=rssPubDate;



3. Nowwhenyourunthegadget,duringtheloop,thegadget

willshowthevalueofrsstitle,alongwiththevalueofi

foreachiteration.

Tomakeitmoremodular,addthisfunctioninaseparate

JavaScriptfile,debug.js,andincludeitinthegadget.html

page:

90







NowlookatthesyntaxoftheWindowsScriptHost'spopup

function:

90



returnValue=WshShell.Popup(Text,[SecondsToWait],[Title],[Type



Thesyntaxofthepopupfunctioncontainsfourparameters.

Table8.1describestheminmoredetail.

Table8.1.ParametersfortheWindowsScript

Host'sPop-upDialog

SNo

Argument

Description Other

1



Text



Stringvalue

containing

thetextin

thepop-up

message



box

2



3

4



SecondsToWait Maximum

lengthof

timeto

displaythe

pop-up

messagein

seconds

Title

Titletext

string

Type

Typeof

buttonsand

icons

(Numeric,

Optional)



Optional,

default=infinite



Optional

These

determinehow

themessage

boxisused.



ThemeaningofthebuttonTypeisdeterminedbycombining

valuesfromTables8.2and8.3.

Table8.2.ButtonTypeValues

Value

Description

0



OKbutton



1



OKandCancelbuttons



2



Abort,Retry,andIgnorebuttons



3



Yes,No,andCancelbuttons



4



YesandNobuttons



5



RetryandCancelbuttons



Table8.3.IconsAssociatedwithButton

Types



Value



Description



16



Stopicon



32



Questionicon



48



Exclamationicon



64



Informationicon



Possiblereturnvalues,indicatingwhichbuttonwasclicked,are

listedinTable8.4.

Table8.4.ReturnValueofthepopup

Function

Value

Description

1



OKbutton



2



Cancelbutton



3



Abortbutton



4



Retrybutton



5



Ignorebutton



6



Yesbutton



7



Nobutton



Iftheuserdoesnotclickabuttonbeforethedialogexpires(as

determinedbytheSecondsToWaitparameter),thereturnvalue

is-1.

Knowledgeoftheseparametersandreturnvaluesenablesthe

usertoimplementadvancedconditionalexecutionswhile

debugging.Userscanimplementaconditionalmessagebox

and,basedonthedifferentoptions,canexcutedifferent

functions.Thishelpslaterinthedebugprocess.



Note

MoreinformationonWindowsScriptHostcanbefound

athttp://msdn2.microsoft.com/enus/library/at5ydy31.aspx.



UsingVisualBasicScript

VisualBasicScript'sMsgBoxfunctioncanbeusedfordebugging

purposes.TouseVBScriptasadebugoptionfromthesample

codeyouhavetodothefollowing:

1. Addadebug.vbsfilewiththedesireddebugfunctions.

2. Includethegadget'sdebug.vbsfilealongwithcodefile

(gadget.html):

90



3. Callthefunctioninthegadget'scodefile(gadget.js).

Ihaveincluded,withthisbook'sothersupplementalmaterials,

adebug.vbsfilethatcanbeusedforthispurpose.Typicallythe

followingfourfunctionscanbehelpfulwithdebugging,whichof

coursecanbeextendedfurtherdependingonyourneeds:

Message,todisplayavalueinapop-updialog

Assert,tocheckthevalidityofanexpression

Question,aconditionalmessagebox;furtherexecution

dependsontheuser'sselection

MessageDialog,todisplayamessagewithcustomized

captiontext.



Youcanintegratethesescriptsintoanygadgetbyincludingthe

.vbsfile(debug.vbs)inthegadget.html.Itworksgreatfor

valuesofvariablesatdifferentpointsofexecutioninsidethe

gadget.Hereisthecodeforthefourfunctionsofdebug.vbs:

90



'////////////////////////////////////////////

'TheDebug.vbsfile'VBScriptFile

'////////////////////////////////////////////



SubMessage(alertmessage)'Foramodaldialogboxtodisplayva

MsgBoxalertmessage,64,"InnovatewithGadgets"

EndSub



Thisfunctionisusedtodisplayavalueinapop-updialog.

Figure8.3furtherdetailshowtheVisualBasicMessagefunction

isusedinthegadget.

90



SubAssert(exp,message)'Toevaluateanexpression

if(notexp)then

MessageDialogmessage,48

endif

EndSub



TheAssertfunctionisusedtoevaluateanexpressionand

checkitsvalidity.

90



FunctionQuestion(alertmessage)'Tobranchexecutionasperuse

Question=MsgBox(alertmessage,1,"InnovatewithGadgets")

EndFunction



TheQuestionfunctionhelpsincreatingconditionalexecution

basedonuser'sselection.

90



SubMessageDialog(alertmessage,dialog)

'Todisplaymultipledebugmessageswithdifferentdialogoptio

MsgBoxalertmessage,dialog,"InnovatewithGadgets"

Endsub



Figure8.3.UsingtheMessagefunctionfromdebug.vbsto

displaythevalueofavariable.

[Viewfullsizeimage]



Let'sgothroughthedetailedsyntaxofMsgBoxfunctionalityin

VisualBasicScript.

90



MsgBox(prompt[,buttons][,title])



Thethreemostcommonparametersandtheirdescriptionsare

listedhere:

prompt(Required)—Themessagetoshowinthemessage

box.Maximumlengthis1,024characters.Youcanseparate



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Chapter 8. Debugging and Deploying a Gadget

Tải bản đầy đủ ngay(0 tr)

×