Skip to content

Instantly share code, notes, and snippets.

@fiznool
fiznool / hashid.js
Created November 16, 2014 09:45
Short 'hash' ID generator.
'use strict';
/**
* The default alphabet is 25 numbers and lowercase letters.
* Any numbers that look like letters and vice versa are removed:
* 1 l, 0 o.
* Also the following letters are not present, to prevent any
* expletives: cfhistu
*/
var ALPHABET =
@fiznool
fiznool / q-extra.decorator.js
Created September 25, 2014 10:48
Add extra functions to the $q Angular service.
'use strict';
angular.module('q-extra', [])
.config(['$provide', function($provide) {
$provide.decorator('$q', ['$delegate', function($delegate) {
if(angular.isUndefined($delegate.resolve)) {
$delegate.resolve = function($q) {
return function(val){
var dfd = $q.defer();
dfd.resolve(val);
@fiznool
fiznool / convert-json-to-js.js
Created September 9, 2014 11:19
Converts a directory of JSON files to JS ones.
'use strict';
var util = require('util'),
fs = require('fs');
var path = './server/services/defs';
var dirs = fs.readdirSync(path);
dirs.forEach(function(dir) {
var servicePath = path + '/' + dir;
@fiznool
fiznool / app.js
Last active August 29, 2015 14:01 — forked from silas/app.js
var express = require('express'),
app = express();
app.configure(function() {
app.use(express.bodyParser());
app.use(app.router);
});
app.get('/', function(req, res) {
res.send(500);
@fiznool
fiznool / fz.restsource.js
Created April 17, 2014 14:27
Patch for angularjs $resource to send POST or PUT depending on ID field
// This patches the $resource.$save function
// to make a PUT when an ID is present.
// Inspired by http://kirkbushell.me/angular-js-using-ng-resource-in-a-more-restful-manner/
'use strict';
angular.module( 'fz.restsource', [ 'ngResource' ] )
.factory('$restsource', function($resource) {
var idField = '_id';
@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();
};
@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 / 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 / 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 / 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" ]