lookupService YahooLocalService{@restbinding};
@restbinding 內容指示服務存取詳細資料位於程式碼中,而不是位於 EGL 部署描述子中。決策很便利,但不可改變。 變更服務位置需要變更原始碼。第 14 課建立了 EGL 部署描述子,其中可能要放置大部分開發工作中的服務存取詳細資料。
function checkForEnter(event Event in)
if(event.ch == 13)
search();
end
end
請考量下列背景詳細資料:EGL 執行時期程式碼會呼叫 checkForEnter 函數並傳遞事件物件,此事件物件是包含事件相關詳細資料的記憶體結構。在此情況下,導致呼叫的事件是 onKeyDown,並且事件物件包括用於代表使用者按鍵的 ASCII 字元。具體而言,數字 13 在 ASCII 表中是換行(ENTER 鍵)的十進位值,如下所註:ASCII 表和說明 (http://www.asciitable.com)。
如果文字欄位具有焦點,則只有當使用者按下某個鍵(例如 Tab 鍵或 ENTER 鍵)時,才呼叫 checkForEnter 函數。只有當鍵是 ENTER 鍵時,函數才會呼叫 search 函數。不久,您將建立 search 函數。
function buttonClicked(event Event in) search(); end
buttonClicked 函數及其與按鈕專屬 onClick 內容的關係,確保使用者按一下搜尋按鈕時會呼叫 search 函數。
function search()
localMap.zoom = 13;
// show an initial marker, as necessary to display the map at all
// (the marker identifies only the zip code)
localMap.addMarker(new MapMarker
{address = zipField.text, description = "zipcode: " + zipField.text});
// Call the remote Yahoo! service, passing the zip code
call lookupService.getSearchResults("YahooDemo", zipField.text)
returning to showResults onException displayError;
end
在特定郵遞區號的這個起始顯示畫面中,您提供給 localMap.addMarker() 函數的唯一詳細資料就是郵遞區號本身。稍後,您將設定地址 內容以提供更多詳細資料,此內容用於顯示地址專屬的標記。
function showResults(retResult ResultSet in)
linkListing HyperLink[0];
for(i INT from 1 to retResult.result.getSize() by 1)
newLink HyperLink{padding = 4, text = retResult.result[i].title, href = "#"};
newLink.setAttribute("address", retResult.result[i].Address + ", "
+ retResult.result[i].city + ", " + retResult.result[i].state);
newLink.setAttribute("title", retResult.result[i].Title);
newLink.onClick ::= mapAddress;
linkListing.appendElement(newLink);
end
listingBox.setChildren(linkListing);
end
newLink HyperLink{padding = 4, text = retResult.result[i].title, href = "#"};
超鏈結會導致呼叫程式碼,而不是呼叫網址。 不過,位置保留元的顯示狀態可確保超鏈結以類似方式顯示文字,即具有底線和顏色,猶如使用者按一下超鏈結可開啟網站。
function mapAddress(e Event in)
// Show the marker when the user clicks the link
businessAddress STRING = e.widget.getAttribute("address") as STRING;
businessName STRING = e.widget.getAttribute("title") as STRING;
localMap.addMarker(new MapMarker {
address = businessAddress,
description = businessName} );
end
當使用者在執行時期按一下超鏈結時,mapAddress 函數會擷取設定於 showResults 函數中的屬性,並在顯示的地圖上設定標記。
function displayError(ex AnyException in)
DojoDialogLib.showError("Yahoo Service",
"Cannot invoke Yahoo Local Service: "
+ ex.message, null);
end
DojoDialogLib 是 com.ibm.egl.rui.dojo.samples 專案中的「程式庫」組件,您已在第 2 課中將該組件新增至工作區。該程式庫中的 showError 函數會在對話框中顯示資訊。函數呼叫包括名為 message 的字串,在異常狀況記錄中,此字串是 EGL 執行時期程式碼傳遞至 displayError 函數的內容。
由於此 Portlet 獨立工作,所以您可以單獨對其進行測試。
10001此郵遞區號指的是曼哈頓市中心區。

與「Yahoo! 在地搜尋服務」相關的問題可以在這裡報告:本端 API - 一般問題,網址為:http://developer.yahoo.net/forum。
localMap.removeAllMarkers();
在下一節課程中,您將在應用程式中內含新的處理程式。