Skip to content

Instantly share code, notes, and snippets.

@raihan004
Created November 25, 2020 07:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raihan004/a36163fac2510c9d043a0f9c8258f9d4 to your computer and use it in GitHub Desktop.
Save raihan004/a36163fac2510c9d043a0f9c8258f9d4 to your computer and use it in GitHub Desktop.
search json data via ajax
// method one
fetch(`/search?view=json&q=tee}`)
.then(response => response.json())
.then(data => console.log(data));
// method 2
const search = async e => {
const query ="cuff";
const {results, count} = await getResults(query);
//const html = results.map(generateItemHtml).join('');
//$('.list-view-items').empty().append(html);
console.log(query, count, results);
}
{% layout none %}
{% assign limit = 10 %}
{% assign product_count = 0 %}
{% assign article_count = 0 %}
{% assign page_count = 0 %}
{% assign has_more = false %}
{% assign first = true %}
{% comment %}
Get first `limit` items from each category. `has_more` is true if some
result was filtered out because of the limit.
{% endcomment %}
{% capture results %}
{% paginate search.results by 9999 %}
{% for item in search.results %}
{% assign skip = false %}
{% assign price_html = "" %}
{% if item.object_type == 'article' %}
{% if article_count >= limit %}
{% assign skip = true %}
{% else %}
{% assign article_count = article_count | plus: 1 %}
{% endif %}
{% elsif item.object_type == 'page' %}
{% if page_count >= limit %}
{% assign skip = true %}
{% else %}
{% assign page_count = page_count | plus: 1 %}
{% endif %}
{% elsif item.object_type == 'product' %}
{% if product_count >= limit %}
{% assign skip = true %}
{% else %}
{% assign product_count = product_count | plus: 1 %}
{% assign product = item %}
{% capture price_html %}
{% render 'product-grid-item-price', product: product, collection: collection, show_reviews: false %}
{% endcapture %}
{% capture unit_price_html %}
{% assign first_variant = product.variants[0] %}
{% render 'unit-price-measurement-variant', variant: first_variant %}
{% endcapture %}
{% endif %}
{% else %}
{% assign skip = true %}
{% endif %}
{% if skip %}
{% assign has_more = true %}
{% else %}
{% unless first %},{% endunless %}
{% assign first = false %}
{
"title": {{ item.title | json }},
{% if item.object_type == 'product' %}
"url": {{ item.url | within: product.collections.last | json }},
{% else %}
"url": {{ item.url | json }},
{% endif %}
{% if item.media and item.media.size > 0 %}
"image": {{ item.media[0].preview_image | product_img_url: '140x' | json }},
{% assign imageResponsive = item.media[0].preview_image | product_img_url: '1000x1000' | replace: '_1000x1000.', '_{width}.' %}
{% elsif item.featured_image.src %}
"image": {{ item.featured_image.src | product_img_url: '140x' | json }},
{% assign imageResponsive = item.featured_image.src | product_img_url: '1000x1000' | replace: '_1000x1000.', '_{width}.' %}
{% elsif item.object_type == 'article' and item.image %}
"image": "{{ item | img_url: '140x' }}",
{% assign imageResponsive = item | img_url: '1000x1000' | replace: '_1000x1000.', '_{width}.' %}
{% endif %}
"imageResponsive": {{ imageResponsive | json }},
"content": {{ item.content | strip_html | truncate: 100 | json }},
"type": "{{ item.object_type }}",
"price_html": {{ price_html | json }},
"unit_price_html": {{ unit_price_html | json }}
}
{% endif %}
{% endfor %}
{% endpaginate %}
{% endcapture %}
{
"terms": {{ search.terms | json }},
"has_more": {{ has_more | json }},
"results": [{{ results }}]
}
{% layout none %}
{% paginate search.results by 99 %}
{% capture results %}
{% for item in search.results %}
{
"title": {{ item.title | highlight: search.terms | json }},
"url": {{ item.url | within: item.collections.last | json }},
"price": {{ item.price | json }},
"featured_image": {{ item.featured_image.src | json }}
}
{% unless forloop.last %},{% endunless %}
{% endfor %}
{% endcapture %}
{
"count": {{ search.results_count }},
"results": [{{ results }}]
}
{% endpaginate %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment