|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>Example: Remote Data via JSONP</title> |
|
|
6 |
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic"> |
|
|
7 |
<link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css"> |
|
|
8 |
<link rel="stylesheet" href="../assets/css/main.css"> |
|
|
9 |
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css"> |
|
|
10 |
<link rel="shortcut icon" type="image/png" href="../assets/favicon.png"> |
|
|
11 |
<script src="../../build/yui/yui-min.js"></script> |
|
|
12 |
|
|
|
13 |
</head> |
|
|
14 |
<body> |
|
|
15 |
<!-- |
|
|
16 |
<a href="https://github.com/yui/yui3"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a> |
|
|
17 |
--> |
|
|
18 |
<div id="doc"> |
|
|
19 |
<div id="hd"> |
|
|
20 |
<h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1> |
|
|
21 |
</div> |
|
|
22 |
|
|
|
23 |
|
|
|
24 |
<h1>Example: Remote Data via JSONP</h1> |
|
|
25 |
<div class="yui3-g"> |
|
|
26 |
<div class="yui3-u-3-4"> |
|
|
27 |
<div id="main"> |
|
|
28 |
<div class="content"><div class="intro"> |
|
|
29 |
<p> |
|
|
30 |
This example demonstrates how to provide autocomplete suggestions from a remote JSONP API. In this case, we're using the YUI Library website's search API to suggest YUI module names. |
|
|
31 |
</p> |
|
|
32 |
|
|
|
33 |
<p> |
|
|
34 |
Try typing in a YUI module name. If you cannot think of any, try typing one of these: node, widget, autocomplete. |
|
|
35 |
</p> |
|
|
36 |
</div> |
|
|
37 |
|
|
|
38 |
<div class="example"> |
|
|
39 |
<div id="demo" class="yui3-skin-sam"> <!-- You need this skin class --> |
|
|
40 |
<label for="ac-input">Enter a YUI module name:</label><br> |
|
|
41 |
<input id="ac-input" type="text" size="45"> |
|
|
42 |
</div> |
|
|
43 |
|
|
|
44 |
<script> |
|
|
45 |
YUI().use('array-extras', 'autocomplete', 'autocomplete-highlighters', function (Y) { |
|
|
46 |
function locateModules(response) { |
|
|
47 |
var results = (response && response.data && response.data.results) || []; |
|
|
48 |
|
|
|
49 |
return Y.Array.filter(results, function (result) { |
|
|
50 |
return result._type === 'module'; |
|
|
51 |
}); |
|
|
52 |
} |
|
|
53 |
|
|
|
54 |
Y.one('#ac-input').plug(Y.Plugin.AutoComplete, { |
|
|
55 |
resultHighlighter: 'phraseMatch', |
|
|
56 |
resultListLocator: locateModules, |
|
|
57 |
resultTextLocator: 'name', |
|
|
58 |
source: 'http://yuilibrary.com/api/v1/search/suggest?q={query}&callback={callback}&count=50' |
|
|
59 |
}); |
|
|
60 |
}); |
|
|
61 |
</script> |
|
|
62 |
|
|
|
63 |
</div> |
|
|
64 |
|
|
|
65 |
<h2>HTML</h2> |
|
|
66 |
<p> |
|
|
67 |
<strong>Note:</strong> be sure to add the <code>yui3-skin-sam</code> classname to the |
|
|
68 |
page's <code><body></code> element or to a parent element of the widget in order to apply |
|
|
69 |
the default CSS skin. See <a href="http://yuilibrary.com/yui/docs/tutorials/skins/">Understanding Skinning</a>. |
|
|
70 |
</p> |
|
|
71 |
<pre class="code prettyprint"><div id="demo" class="yui3-skin-sam"> <!-- You need this skin class --> |
|
|
72 |
<label for="ac-input">Enter a YUI module name:</label><br> |
|
|
73 |
<input id="ac-input" type="text" size="45"> |
|
|
74 |
</div></pre> |
|
|
75 |
|
|
|
76 |
|
|
|
77 |
<h2>JavaScript</h2> |
|
|
78 |
|
|
|
79 |
<h3>YUI Library Search Response Data</h3> |
|
|
80 |
|
|
|
81 |
<p> |
|
|
82 |
The YUI Library website's search API returns a JavaScript object that looks like this: |
|
|
83 |
</p> |
|
|
84 |
|
|
|
85 |
<pre class="code prettyprint">{ |
|
|
86 |
"status": "success", |
|
|
87 |
"data": { |
|
|
88 |
"query": "node", |
|
|
89 |
"total": 218, |
|
|
90 |
"maxScore": 153.57176, |
|
|
91 |
"took": 3, |
|
|
92 |
"results": [ |
|
|
93 |
{ |
|
|
94 |
"_index": "docs", |
|
|
95 |
"_type": "component", |
|
|
96 |
"_id": "component#node", |
|
|
97 |
"_score": 153.57176, |
|
|
98 |
"name": "node", |
|
|
99 |
"displayName": "Node", |
|
|
100 |
"description": "Provides a wrapper for working with DOM nodes.", |
|
|
101 |
"author": "msweeney", |
|
|
102 |
"url": "/yui/docs/node/" |
|
|
103 |
}, |
|
|
104 |
|
|
|
105 |
... |
|
|
106 |
] |
|
|
107 |
} |
|
|
108 |
}</pre> |
|
|
109 |
|
|
|
110 |
|
|
|
111 |
<p> |
|
|
112 |
If the response were a simple array of strings, AutoComplete would interpret it correctly by default. However, in this case, the response is an object that contains a <code>data.results</code> property, the value of which is an array of result objects rather than an array of strings. Furthermore, we only want results whose <code>_type</code> property is <code>"module"</code>. |
|
|
113 |
</p> |
|
|
114 |
|
|
|
115 |
<p> |
|
|
116 |
This means we'll need to specify a <a href="http://yuilibrary.com/yui/docs/api/classes/AutoCompleteBase.html#attr_resultListLocator"><code>resultListLocator</code></a> to filter down the JSONP response to just an array of module results. Additionally we'll provide a values for <a href="http://yuilibrary.com/yui/docs/api/classses/AutoCompleteBase.html#attr_resultTextLocator"><code>resultTextLocator</code></a> to indicate the property on <em>each</em> result object that contains the suggestion text, as demonstrated in the implementation code below. |
|
|
117 |
</p> |
|
|
118 |
|
|
|
119 |
<h3>Implementation</h3> |
|
|
120 |
|
|
|
121 |
<pre class="code prettyprint">YUI().use('array-extras', 'autocomplete', 'autocomplete-highlighters', function (Y) { |
|
|
122 |
function locateModules(response) { |
|
|
123 |
var results = (response && response.data && response.data.results) || []; |
|
|
124 |
|
|
|
125 |
return Y.Array.filter(results, function (result) { |
|
|
126 |
return result._type === 'module'; |
|
|
127 |
}); |
|
|
128 |
} |
|
|
129 |
|
|
|
130 |
Y.one('#ac-input').plug(Y.Plugin.AutoComplete, { |
|
|
131 |
resultHighlighter: 'phraseMatch', |
|
|
132 |
resultListLocator: locateModules, |
|
|
133 |
resultTextLocator: 'name', |
|
|
134 |
source: 'http://yuilibrary.com/api/v1/search/suggest?q={query}&callback={callback}&count=50' |
|
|
135 |
}); |
|
|
136 |
});</pre> |
|
|
137 |
|
|
|
138 |
|
|
|
139 |
<h2>Complete Example Source</h2> |
|
|
140 |
|
|
|
141 |
<pre class="code prettyprint"><div id="demo" class="yui3-skin-sam"> <!-- You need this skin class --> |
|
|
142 |
<label for="ac-input">Enter a YUI module name:</label><br> |
|
|
143 |
<input id="ac-input" type="text" size="45"> |
|
|
144 |
</div> |
|
|
145 |
|
|
|
146 |
<script> |
|
|
147 |
YUI().use('array-extras', 'autocomplete', 'autocomplete-highlighters', function (Y) { |
|
|
148 |
function locateModules(response) { |
|
|
149 |
var results = (response && response.data && response.data.results) || []; |
|
|
150 |
|
|
|
151 |
return Y.Array.filter(results, function (result) { |
|
|
152 |
return result._type === 'module'; |
|
|
153 |
}); |
|
|
154 |
} |
|
|
155 |
|
|
|
156 |
Y.one('#ac-input').plug(Y.Plugin.AutoComplete, { |
|
|
157 |
resultHighlighter: 'phraseMatch', |
|
|
158 |
resultListLocator: locateModules, |
|
|
159 |
resultTextLocator: 'name', |
|
|
160 |
source: 'http://yuilibrary.com/api/v1/search/suggest?q={query}&callback={callback}&count=50' |
|
|
161 |
}); |
|
|
162 |
}); |
|
|
163 |
</script></pre> |
|
|
164 |
|
|
|
165 |
</div> |
|
|
166 |
</div> |
|
|
167 |
</div> |
|
|
168 |
|
|
|
169 |
<div class="yui3-u-1-4"> |
|
|
170 |
<div class="sidebar"> |
|
|
171 |
|
|
|
172 |
|
|
|
173 |
|
|
|
174 |
<div class="sidebox"> |
|
|
175 |
<div class="hd"> |
|
|
176 |
<h2 class="no-toc">Examples</h2> |
|
|
177 |
</div> |
|
|
178 |
|
|
|
179 |
<div class="bd"> |
|
|
180 |
<ul class="examples"> |
|
|
181 |
|
|
|
182 |
|
|
|
183 |
<li data-description="How to provide autocomplete suggestions from a local array."> |
|
|
184 |
<a href="ac-local.html">Basic Local Data</a> |
|
|
185 |
</li> |
|
|
186 |
|
|
|
187 |
|
|
|
188 |
|
|
|
189 |
<li data-description="How to provide autocomplete suggestions using a remote JSONP source."> |
|
|
190 |
<a href="ac-jsonp.html">Remote Data via JSONP</a> |
|
|
191 |
</li> |
|
|
192 |
|
|
|
193 |
|
|
|
194 |
|
|
|
195 |
<li data-description="How to provide autocomplete suggestions using a YQL query source."> |
|
|
196 |
<a href="ac-yql.html">Remote Data via YQL</a> |
|
|
197 |
</li> |
|
|
198 |
|
|
|
199 |
|
|
|
200 |
|
|
|
201 |
<li data-description="How to provide autocomplete suggestions using a DataSource instance."> |
|
|
202 |
<a href="ac-datasource.html">Remote Data via DataSource</a> |
|
|
203 |
</li> |
|
|
204 |
|
|
|
205 |
|
|
|
206 |
|
|
|
207 |
<li data-description="How to implement delimited tag completion."> |
|
|
208 |
<a href="ac-tagging.html">Tag Completion Using Query Delimiters</a> |
|
|
209 |
</li> |
|
|
210 |
|
|
|
211 |
|
|
|
212 |
|
|
|
213 |
<li data-description="How to find and select Flickr photos using a YQL source and a custom autocomplete result formatter."> |
|
|
214 |
<a href="ac-flickr.html">Find Photos on Flickr (Custom Formatting, YQL Source)</a> |
|
|
215 |
</li> |
|
|
216 |
|
|
|
217 |
|
|
|
218 |
|
|
|
219 |
<li data-description="How to use autocomplete-base to filter a set of existing items on a page."> |
|
|
220 |
<a href="ac-filter.html">Filter a Set of Existing Items on a Page</a> |
|
|
221 |
</li> |
|
|
222 |
|
|
|
223 |
|
|
|
224 |
|
|
|
225 |
<li data-description="How to find an address using a YQL source calling Google's Geocoding Service"> |
|
|
226 |
<a href="ac-geocode.html">Address Completion on Google's Geocoding Service</a> |
|
|
227 |
</li> |
|
|
228 |
|
|
|
229 |
|
|
|
230 |
</ul> |
|
|
231 |
</div> |
|
|
232 |
</div> |
|
|
233 |
|
|
|
234 |
|
|
|
235 |
|
|
|
236 |
</div> |
|
|
237 |
</div> |
|
|
238 |
</div> |
|
|
239 |
</div> |
|
|
240 |
|
|
|
241 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
242 |
<script>prettyPrint();</script> |
|
|
243 |
|
|
|
244 |
<script> |
|
|
245 |
YUI.Env.Tests = { |
|
|
246 |
examples: [], |
|
|
247 |
project: '../assets', |
|
|
248 |
assets: '../assets/autocomplete', |
|
|
249 |
name: 'ac-jsonp', |
|
|
250 |
title: 'Remote Data via JSONP', |
|
|
251 |
newWindow: '', |
|
|
252 |
auto: false |
|
|
253 |
}; |
|
|
254 |
YUI.Env.Tests.examples.push('ac-local'); |
|
|
255 |
YUI.Env.Tests.examples.push('ac-jsonp'); |
|
|
256 |
YUI.Env.Tests.examples.push('ac-yql'); |
|
|
257 |
YUI.Env.Tests.examples.push('ac-datasource'); |
|
|
258 |
YUI.Env.Tests.examples.push('ac-tagging'); |
|
|
259 |
YUI.Env.Tests.examples.push('ac-flickr'); |
|
|
260 |
YUI.Env.Tests.examples.push('ac-filter'); |
|
|
261 |
YUI.Env.Tests.examples.push('ac-geocode'); |
|
|
262 |
|
|
|
263 |
</script> |
|
|
264 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
265 |
|
|
|
266 |
|
|
|
267 |
|
|
|
268 |
</body> |
|
|
269 |
</html> |