Skip to content

Commit 1a5e441

Browse files
committed
MAGE-1000 Centralize Mustache logic
1 parent 787a82a commit 1a5e441

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

view/frontend/web/js/instantsearch.js

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,24 @@ define([
88
'algoliaInsights',
99
'algoliaHooks',
1010
], function ($, algoliaBundle, Hogan, Mustache, priceUtils) {
11+
const processTemplate = (template, templateVars, useMustache = false) => {
12+
const hoganStart = performance.now();
13+
const wrapperTemplate = Hogan.compile(template);
14+
const hoganResult = wrapperTemplate.render(templateVars);
15+
const hoganEnd = performance.now();
16+
console.log("Hogan execution time: %s ms", hoganEnd - hoganStart);
17+
18+
if (useMustache) {
19+
const mustacheStart = performance.now();
20+
const mustacheResult = Mustache.render(template, templateVars);
21+
const mustacheEnd = performance.now();
22+
console.log("Mustache execution time: %s ms", mustacheEnd - mustacheStart);
23+
return mustacheResult;
24+
}
25+
26+
return hoganResult;
27+
};
28+
1129
$(function ($) {
1230
/** We have nothing to do here if instantsearch is not enabled **/
1331
if (
@@ -109,21 +127,9 @@ define([
109127
config : algoliaConfig.instant,
110128
translations : algoliaConfig.translations,
111129
};
112-
const hoganStart = performance.now();
113-
const wrapperTemplate = Hogan.compile(template);
114-
const hoganResult = wrapperTemplate.render(templateVars);
115-
const hoganEnd = performance.now();
116-
console.log("Hogan execution time: %s ms", hoganEnd - hoganStart);
117-
118-
const mustacheStart = performance.now();
119-
const mustacheResult = Mustache.render(template, templateVars);
120-
const mustacheEnd = performance.now();
121-
console.log("Mustache execution time: %s ms", mustacheEnd - mustacheStart);
122-
130+
123131
$('.algolia-instant-selector-results')
124-
.html(
125-
mustacheResult
126-
)
132+
.html(processTemplate(template, templateVars, true))
127133
.show();
128134

129135
/**
@@ -348,17 +354,15 @@ define([
348354
container: '#algolia-stats',
349355
templates: {
350356
text: function (data) {
351-
var hoganTemplate = Hogan.compile(
352-
$('#instant-stats-template').html()
353-
);
354-
355357
data.first = data.page * data.hitsPerPage + 1;
356358
data.last = Math.min(
357359
data.page * data.hitsPerPage + data.hitsPerPage,
358360
data.nbHits
359361
);
360362
data.seconds = data.processingTimeMS / 1000;
361363
data.translations = window.algoliaConfig.translations;
364+
365+
// TODO: Revisit this injected jQuery logic
362366
const searchParams = new URLSearchParams(window.location.search);
363367
const searchQuery = searchParams.has('q') || '';
364368
if (searchQuery === '' && !algoliaConfig.isSearchPage) {
@@ -368,7 +372,9 @@ define([
368372
$('.algolia-instant-replaced-content').hide();
369373
$('.algolia-instant-selector-results').show();
370374
}
371-
return hoganTemplate.render(data);
375+
376+
var template = $('#instant-stats-template').html();
377+
return processTemplate(template, data, true);
372378
},
373379
},
374380
},

0 commit comments

Comments
 (0)