From e0158c5fff0b1a36f7be36f08373efb0bd1dfe52 Mon Sep 17 00:00:00 2001 From: agusmakmun Date: Wed, 18 Jan 2017 14:54:52 +0700 Subject: [PATCH] Fixed single object - Bug:#4 --- static/js/super-search.js | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/static/js/super-search.js b/static/js/super-search.js index 1476612..445a80d 100644 --- a/static/js/super-search.js +++ b/static/js/super-search.js @@ -110,11 +110,18 @@ } searchResultsEl.style.offsetWidth; - var matchingPosts = posts.filter(function (post) { - if ((post.title + '').toLowerCase().indexOf(currentInputValue) !== -1 || (post.description + '').toLowerCase().indexOf(currentInputValue) !== -1) { - return true; - } - }); + var matchingPosts; + // check the `posts` object is single or many objects. + // if posts.title === undefined, so posts is many objects. + if(posts.title === undefined) { + matchingPosts = posts.filter(function (post) { + if ((post.title + '').toLowerCase().indexOf(currentInputValue) !== -1 || (post.description + '').toLowerCase().indexOf(currentInputValue) !== -1) { + return true; + } + }); + }else { + matchingPosts = [posts]; // assign single object to Array + } if (!matchingPosts.length) { searchResultsEl.classList.add('is-hidden'); } @@ -129,4 +136,4 @@ lastSearchResultHash = currentResultHash; }); -})(); \ No newline at end of file +})();