This repository has been archived by the owner on Mar 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 235
/
mkt_suggestions.js
52 lines (47 loc) · 1.86 KB
/
mkt_suggestions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Init site search suggestions and populate the suggestions container.
(function() {
// MKT search init.
if (!z.capabilities.gaia || z.enableSearchSuggestions) { // Disable suggestions on Gaia for now.
$('#search #search-q').searchSuggestions($('#site-search-suggestions'),
processResults, 'MKT');
}
var previous_request;
function processResults(settings) {
if (!settings) {
return;
}
var li_item = template(
'<li><a href="{url}"><span>{name}</span></a></li>'
);
// Note that if ajaxCache doesn't need to make a new request, it will
// return `undefined`.
var new_request = $.ajaxCache({
url: settings['$results'].attr('data-src'),
data: settings['$form'].serialize() + '&cat=' + settings.category,
newItems: function(formdata, items) {
var eventName;
if (items !== undefined) {
var ul = '';
$.each(items, function(i, item) {
var d = {
url: escape_(item.url) || '#'
};
if (item.name) {
d.name = escape_(item.name);
// Append the item only if it has a name.
ul += li_item(d);
}
});
settings['$results'].find('ul').html(ul);
}
settings['$results'].addClass('visible')
.trigger('resultsUpdated', [items]);
$('#site-header').addClass('suggestions');
}
});
if (previous_request) {
previous_request.abort();
}
previous_request = new_request;
}
})();