Skip to content

Instantly share code, notes, and snippets.

@fiznool
fiznool / Underscore-Mustache
Created March 23, 2012 10:44
Underscore Mustache-like templating
// Change underscore's templating from ERB-style to Mustache-style
_.templateSettings = {
interpolate : /\{\{(.+?)\}\}/g
};
@fiznool
fiznool / flotr2-amd.js
Created July 3, 2012 14:37
Flotr2 AMD
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['bean', 'underscore'], function (bean, _) {
// Also create a global in case some scripts
// that are loaded still are looking for
// a global even when an AMD loader is in use.
return (root.Flotr2 = factory(bean, _));
});
} else {
@fiznool
fiznool / serializeJSON.js
Created June 2, 2013 19:54
Serialize a Backbone Form to JSON.
return _.reduce($el.serializeArray(), function (hash, pair) {
hash[pair.name] = pair.value;
return hash;
}, {});
@fiznool
fiznool / parseQueryParams.js
Last active December 18, 2015 00:09
Parses a query string into a JSON object.
var parseQueryParams = function(params) {
if(!params) { return {}; }
return _.reduce(params.split('&'), function (hash, param) {
var p = param.split('=');
hash[p[0]] = p[1];
return hash;
}, {});
};
@fiznool
fiznool / emailRegex.js
Last active December 18, 2015 00:09
HTML5 Email validation regex
var EMAIL_REGEX = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,253}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,253}[a-zA-Z0-9])?)*$/;
var isValidEmailAddress = function(email) {
return EMAIL_REGEX.test(email);
};
@fiznool
fiznool / validator.js
Created June 4, 2013 07:35
Module which augments backbone.validator
define(function(require, exports, module) {
var _ = require('lodash');
var Validator = require('backbone.validator');
// Duck-punch so that we get the errors in the right format:
// Instead of
// {
// field1: [ "errormsg1", "errormsg2" ],
// field2: [ "errormsg3" ]
@fiznool
fiznool / sync.js
Created June 5, 2013 09:21
Overriding Backbone.sync
// Store a copy of the original Backbone.sync
var originalSync = Backbone.sync;
// Override Backbone.sync for all future requests.
Backbone.sync = function(method, model, options) {
// Do something special here
// And then call the original sync
return originalSync.call(model, method, model, options);
};
@fiznool
fiznool / backboneConstructor.js
Created June 6, 2013 09:19
Overriding Backbone Constructor.
Backbone.View.extend({
constructor: function (options) {
// Do stuff before the View has been setup
Backbone.View.apply(this, arguments);
// Do stuff after the View has been setup,
// particularly after `initialize()` has been called
return this;
@fiznool
fiznool / payload.js
Created June 10, 2013 21:11
Method to only send some Backbone Model attributes to server.
var UserModel = Backbone.Model.extend({
defaults: {
username: "",
password: {
old: "",
new: "",
confirm: ""
}
},
@fiznool
fiznool / promises.js
Created July 31, 2013 08:41
Wrapper for jQuery promises to make them behave a little more like rsvp.js. Depends on jQuery and Underscore.
exports.promise = function(work) {
var dfd = $.Deferred();
work(dfd.resolve, dfd.reject);
return dfd.promise();
};
exports.promiseResolved = function() {
return $.Deferred().resolve().promise();
};