Skip to content

Instantly share code, notes, and snippets.

@DanWebb
DanWebb / notification.js
Created May 1, 2014 17:56
Simple notification object
var notification = {
element: $('#notification'),
timeout: null,
show: function(type, message) {
'use strict';
// reset the current timer
clearTimeout(this.timeout);
this.element
@DanWebb
DanWebb / collection.liquid
Created May 8, 2014 09:45
Shopify filter code with working example
<!-- add the options +tags to the listed products attributes -->
{% if p.options[0] == 'Color' %}{% capture colours %}{% for variant in p.variants %}{% unless forloop.first %},{% endunless %}{{ variant.options[0] }}{% endfor %}{% endcapture %}{% endif %}
{% if p.options[1] == 'Material' %}{% capture materials %}{% for variant in p.variants %}{% unless forloop.first %},{% endunless %}{{ variant.options[1] }}{% endfor %}{% endcapture %}{% endif %}
{% if p.options[2] == 'Style' %}{% capture styles %}{% for variant in p.variants %}{% unless forloop.first %},{% endunless %}{{ variant.options[2] }}{% endfor %}{% endcapture %}{% endif %}
{% if p.tags %}{% capture tagg %}{% for tag in p.tags %}{% if tag contains 'recommended:' %}{% unless forloop.first %},{% endunless %}{{ tag | replace: 'recommended:', '' }}{% endif %}{% endfor %}{% endcapture %}{% endif %}
<div class="product" data-colours="{{ colours | escape }}" data-materials="{{ materials | escape }}" data-styles="{{ styles | escape }}" data-tags="{{ tagg | esc
@DanWebb
DanWebb / setIntervalTimeout.js
Last active August 29, 2015 14:03
setIntervalTimeout method created to solve issues with elements loading after page load where promises can't be applied.
// call a callback function every *interval* until *stopTime* or until (bool)true is
// returned from the callback function
function setIntervalTimeout(callback, interval, stopTime) {
var i;
i = setInterval(function() {
if(callback()) clearInterval(i);
}, interval);
setTimeout(function() {
@DanWebb
DanWebb / jquery.ba-tinypubsub.js
Last active August 29, 2015 14:05 — forked from cowboy/HEY-YOU.md
jQuery Tiny Pub/Sub
/* jQuery Tiny Pub/Sub - v0.7 - 10/27/2011
* http://benalman.com/
* Copyright (c) 2011 "Cowboy" Ben Alman; Licensed MIT, GPL */
(function($) {
var o = $({});
$.subscribe = function() {
o.on.apply(o, arguments);
@DanWebb
DanWebb / formAJAX.js
Last active July 1, 2016 03:24
API AJAX methods: HTML5 IE10+ form+Image upload ajax, Generic AJAX calls, AJAX from forms.
// ajax call from a form
api.formAjax = function($form, type, callback) {
var data = {};
$form
.find('input:not([type="submit"], [type="file"]), textarea, select')
.each(function() {
data[$(this).attr('name')] = $(this).val();
})
.promise()
.done(function() {
@DanWebb
DanWebb / scroll.html
Last active June 21, 2021 21:01
Shopify infinite scrolling
<!--
a new template will need to be created for this file then this will
be what's called from the ajax by sending it with a property of
view:*template name*.
-->
{% layout none %}
{% paginate items by 5 %}
{% for item in items %}
<!-- item html -->
@DanWebb
DanWebb / related_products.liquid
Last active June 21, 2021 21:01
Shopify related products liquid
@DanWebb
DanWebb / addItems.js
Created January 30, 2015 18:29
Shopify only allows us to make a single call at a time adding one item at a time. Here's a method to add multiple items to the cart at once which works around that.
// add multiple items to the cart by passing an array of item objects
function addItems(items, callback) {
if(!items.length) {
// we ran out of items
if(typeof callback === 'function') callback();
return;
}
$.ajax('/cart/add.js', {
type:'POST',
@DanWebb
DanWebb / wait.js
Last active August 29, 2015 14:15
Set a waiting period on any number of different actions definable by name and time parameters, returns true if the action can be executed or false if it is being called again within the waiting period.
var wait = (function() {
var timers = {};
return function(name, time) {
// if the current timer has not been set yet or it has previously
// been set but finished it's waiting period
if(typeof(timers[name])==='undefined' || timers[name]) {
timers[name] = false;
setTimeout(function() { timers[name] = true; }, time);
return true;
@DanWebb
DanWebb / cart.js
Created February 12, 2015 12:54
General functions for creating AJAX based Shopify Quick carts or cart pages
var Cart = (function($, Shopify) {
var list = '.sproducts';
var total = '.shoppingbag-total';
function updateTotal() {
Shopify.getCart(function(data) {
$(total).text(Shopify.formatMoney(data.total_price, '£\{\{amount}}'));
});
}