|
0
|
1 |
//create a namespace for this example: |
|
|
2 |
YAHOO.namespace("example.GetNews"); |
|
|
3 |
|
|
|
4 |
|
|
|
5 |
//This example uses the "Module Pattern"; a full explanation of |
|
|
6 |
//this pattern can be found on yuiblog: |
|
|
7 |
// http://yuiblog.com/blog/2007/06/12/module-pattern |
|
|
8 |
YAHOO.example.GetNews = function() { |
|
|
9 |
|
|
|
10 |
//set up some shortcuts in case our typing fingers |
|
|
11 |
//get lazy: |
|
|
12 |
var Event = YAHOO.util.Event, |
|
|
13 |
Dom = YAHOO.util.Dom, |
|
|
14 |
JSON = YAHOO.lang.JSON, |
|
|
15 |
Button = YAHOO.widget.Button, |
|
|
16 |
Get = YAHOO.util.Get, |
|
|
17 |
elResults = Dom.get("results"); |
|
|
18 |
|
|
|
19 |
//we won't use the Get Utility's success handler in this example |
|
|
20 |
//because the web service we're using has a built-in callback |
|
|
21 |
//mechanism. But we'll stub it out here and use it to simply |
|
|
22 |
//log a message to the logger: |
|
|
23 |
var onGetNewsSuccess = function() { |
|
|
24 |
YAHOO.log("The Get Utility has fired the success handler indicating that the requested script has loaded and is ready for use.", "info", "example"); |
|
|
25 |
} |
|
|
26 |
|
|
|
27 |
//function to retrieve data from Yahoo! News Search web service -- |
|
|
28 |
//http://developer.yahoo.com/search/news/V1/newsSearch.html |
|
|
29 |
var getNewsData = function() { |
|
|
30 |
YAHOO.log("Button clicked; getGetNewsData firing.", "info", "example"); |
|
|
31 |
|
|
|
32 |
//Load the transitional state of the results section: |
|
|
33 |
elResults.innerHTML = "<h3>Retrieving news search results for for " + |
|
|
34 |
Dom.get("searchString").value + ":</h3>" + |
|
|
35 |
"<img src='http://l.yimg.com/a/i/nt/ic/ut/bsc/busybar_1.gif' " + |
|
|
36 |
"alt='Please wait...'>"; |
|
|
37 |
|
|
|
38 |
//prepare the URL for the Yahoo Site Explorer API: |
|
|
39 |
var sURL = "http://search.yahooapis.com/NewsSearchService/V1/newsSearch?" + |
|
|
40 |
"appid=3wEDxLHV34HvAU2lMnI51S4Qra5m.baugqoSv4gcRllqqVZm3UrMDZWToMivf5BJ3Mom" + |
|
|
41 |
"&results=10&output=json&" + |
|
|
42 |
"&callback=YAHOO.example.GetNews.callback" + |
|
|
43 |
"&query=" + encodeURIComponent(Dom.get("searchString").value); |
|
|
44 |
|
|
|
45 |
//This simple line is the call to the Get Utility; we pass |
|
|
46 |
//in the URL and the configuration object, which in this case |
|
|
47 |
//consists merely of our success and failure callbacks: |
|
|
48 |
var transactionObj = Get.script(sURL, { |
|
|
49 |
onSuccess: onGetNewsSuccess, |
|
|
50 |
scope : this |
|
|
51 |
}); |
|
|
52 |
|
|
|
53 |
//The script method returns a single-field object containing the |
|
|
54 |
//tranaction id: |
|
|
55 |
YAHOO.log("Get Utility transaction started; transaction object: " + YAHOO.lang.dump(transactionObj), "info", "example"); |
|
|
56 |
|
|
|
57 |
} |
|
|
58 |
|
|
|
59 |
return { |
|
|
60 |
init: function() { |
|
|
61 |
//suppress default form behavior |
|
|
62 |
Event.on("newsSearch", "submit", function(e) { |
|
|
63 |
Event.preventDefault(e); |
|
|
64 |
getNewsData(); |
|
|
65 |
}, this, true); |
|
|
66 |
|
|
|
67 |
//instantiate Button: |
|
|
68 |
var oButton = new Button("getNewsData"); |
|
|
69 |
YAHOO.log("GetNews Button instantiated.", "info", "example"); |
|
|
70 |
getNewsData(); |
|
|
71 |
}, |
|
|
72 |
|
|
|
73 |
callback: function(results) { |
|
|
74 |
YAHOO.log("Web service returned data to YAHOO.example.GetNews.callback; beginning to process.", "info", "example"); |
|
|
75 |
//work with the returned data to extract meaningful fields: |
|
|
76 |
var aResults = results.ResultSet.Result; |
|
|
77 |
var totalLinks = results.ResultSet.totalResultsAvailable; |
|
|
78 |
var returnedLinkCount = results.ResultSet.totalResultsReturned; |
|
|
79 |
|
|
|
80 |
if(returnedLinkCount) {//there are inbound links; process and display them: |
|
|
81 |
|
|
|
82 |
//write header and open list of inbound links: |
|
|
83 |
var html = "<h3>There are " + |
|
|
84 |
totalLinks + |
|
|
85 |
" news items on this topic; here are the first " + |
|
|
86 |
returnedLinkCount + |
|
|
87 |
":</h3><ol>"; |
|
|
88 |
|
|
|
89 |
//process list of inbound links: |
|
|
90 |
for (var i=0; i < aResults.length; i++) { |
|
|
91 |
html += "<li><strong><a href='" + |
|
|
92 |
aResults[i].Url + |
|
|
93 |
"'>" + aResults[i].Title + |
|
|
94 |
"</a>:</strong> " + aResults[i].Summary + |
|
|
95 |
"</li>"; |
|
|
96 |
} |
|
|
97 |
|
|
|
98 |
//close list of inbound links |
|
|
99 |
html += "</ol>"; |
|
|
100 |
|
|
|
101 |
} else {//no inbound links exist for this page: |
|
|
102 |
|
|
|
103 |
var html = "<h3>There are no news items available for the topic specified.</h3"; |
|
|
104 |
|
|
|
105 |
} |
|
|
106 |
|
|
|
107 |
//insert string into DOM: |
|
|
108 |
elResults.innerHTML = html; |
|
|
109 |
} |
|
|
110 |
} |
|
|
111 |
}(); |
|
|
112 |
|
|
|
113 |
//Initialize the example when the DOM is completely loaded: |
|
|
114 |
YAHOO.util.Event.onDOMReady( |
|
|
115 |
YAHOO.example.GetNews.init, |
|
|
116 |
YAHOO.example.GetNews, //pass this object to init and... |
|
|
117 |
true); //...run init in the passed object's |
|
|
118 |
//scope |