Skip to content

Instantly share code, notes, and snippets.

View chrisjhoughton's full-sized avatar

Chris Houghton chrisjhoughton

  • Beacon
  • London, UK
View GitHub Profile
@chrisjhoughton
chrisjhoughton / backbone.sync.js
Last active August 14, 2022 02:22
Backbone Sync with jQuery and a REST API
/*
Overview:
- triggers "sync", "sync_success" and "sync_error" messages after sync
- runs this.url() on the model/collection inputted (note - this function is declared by default in models, not collections. See http://backbonejs.org/#Collection-url for more details)
- handles the four main REST api methods: POST, PUT, GET, DELETE
- expects 'create' to return an id, which is auto-set to the newly created model
- automatically sets parameters when 'fetching' models
- automatically adds models to collections when 'fetching' collections
EXAMPLES:
@chrisjhoughton
chrisjhoughton / badbrowser.js
Created May 9, 2013 13:16
Show a message to people in IE8 or lower asking them to upgrade their browser. Depends on jQuery.
(function($) {
var BadBrowser = {
show: function() {
var _this = this;
if (_this.isIE8orLower()) {
_this.showMessage();
}
},
@chrisjhoughton
chrisjhoughton / contains.js
Last active December 17, 2015 04:59
See if an array/string contains something. Nothing fancy, but without a library you'll need this as array.indexOf doesn't work in IE8 and lower.
var contains = function (list, value) {
if (list instanceof Array) {
var i = list.length;
while (i--) {
if (list[i] === value) {
return true;
}
}
return false;
} else if (typeof list === "string") {
@chrisjhoughton
chrisjhoughton / mobile.js
Created May 16, 2013 10:33
A simple function check if a browser is a mobile device or not.
var isMobile = function() {
var check = false;
(function(a) {
if (/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i.test(a) || /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|j
@chrisjhoughton
chrisjhoughton / mini_underscore.js
Created May 16, 2013 13:21
A tiny hacked together version of a few essential Underscore functions. Great for use in small scripts where you don't want to load the entire library.
// Setup the methods
var _ = {
init: function() {
var _this = this;
// Create the comparitor methods e.g. isString
this.each(['Arguments', 'Function', 'String', 'Number', 'Date', 'RegExp'], function(name) {
_this['is' + name] = function(obj) {
return toString.call(obj) == '[object ' + name + ']';
};
@chrisjhoughton
chrisjhoughton / unique.js
Last active December 17, 2015 09:59
A function for getting all the unique values in an array.
var unique = function(array) {
var u = {}, a = [];
for (var i = 0, l = array.length; i < l; ++i) {
if (u.hasOwnProperty(array[i])) {
continue;
}
a.push(array[i]);
u[array[i]] = 1;
}
return a;
@chrisjhoughton
chrisjhoughton / in_array.js
Created May 16, 2013 14:02
See if a value is an array.
var inArray = function(list, value) {
var i = list.length;
while (i--) {
if (list[i] === value) {
return true;
}
}
return false;
};
@chrisjhoughton
chrisjhoughton / handheld.js
Created May 24, 2013 14:37
Detect if it's a mobile or a tablet
var isHandheld = function() {
return ( /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) );
}
@chrisjhoughton
chrisjhoughton / loadScript.js
Last active December 19, 2015 22:29
Load a script
var loadScript = function(url, cb) {
var script = document.createElement("script");
script.type = "text/javascript";
if (script.readyState) { //IE
script.onreadystatechange = function () {
if (script.readyState == "loaded" || script.readyState == "complete") {
script.onreadystatechange = null;
cb();
}
};
@chrisjhoughton
chrisjhoughton / querystring.js
Created September 30, 2013 14:53
A module to get a query parameter
define(function() {
var QueryString = function() {
// This function is anonymous, is executed immediately and
// the return value is assigned to QueryString!
var query_string = {};
var query = window.location.search.substring(1);
// String trailing forward slash if exists
if (query.charAt(query.length - 1) == "/") {