Skip to content

Instantly share code, notes, and snippets.

// This is just an extension of https://github.com/gruntjs/grunt-contrib-connect#middleware
grunt.initConfig({
connect: {
server: {
options: {
middleware: function(connect, options) {
var middlewares = [];
if (!Array.isArray(options.base)) {
options.base = [options.base];
}
@ssafejava
ssafejava / gist:8394497
Last active January 3, 2016 02:19
Backbone link hijacking
// All navigation that is relative should be passed through the navigate
// method, to be processed by the router. If the link has a `data-bypass`
// attribute, bypass the delegation completely.
$(document).on('click', 'a[href]:not([data-bypass])', function(ev) {
// Get the absolute anchor href.
var $link = $(ev.currentTarget);
var href = { prop: $link.prop("href"), attr: $link.attr("href") };
// Get the absolute root.
var root = location.protocol + "//" + location.host + app.root;
// polyfill window.getMatchedCSSRules() in FireFox 6+
if ( typeof window.getMatchedCSSRules !== 'function' ) {
var ELEMENT_RE = /[\w-]+/g,
ID_RE = /#[\w-]+/g,
CLASS_RE = /\.[\w-]+/g,
ATTR_RE = /\[[^\]]+\]/g,
// :not() pseudo-class does not add to specificity, but its content does as if it was outside it
PSEUDO_CLASSES_RE = /\:(?!not)[\w-]+(\(.*\))?/g,
PSEUDO_ELEMENTS_RE = /\:\:?(after|before|first-letter|first-line|selection)/g;
// convert an array-like object to array
@ssafejava
ssafejava / listener.js
Created September 17, 2013 05:05
Wrap a socket in as many transform streams as you like, use the socket as normal.
var net = require('net');
var es = require('event-stream');
var util = require('util');
var _ = require('lodash');
var stream = require('stream');
/**
* A socketWrapper wraps a net.Socket object with any number
* of readableTransforms and writableTransforms, specified in the options object.
* @param {Object} options Options. Options not listed below are forwarded to net.Socket.
@ssafejava
ssafejava / backbone.eventSafety.js
Last active December 20, 2015 13:29
Throw errors when binding null events in Backbone.
/*global Backbone: true, _:true */
(function() {
"use strict";
/**
* Backbone.eventSafety
*
* Simple plugin that throws an error when Backbone.Events.on & Backbone.Events.once are called
* without a callback. Because you might be counting on that and accidentally pass it undefined
* because of a scoping error.
* Why Backbone doesn't do that, only God and @jashkenas knows.
@ssafejava
ssafejava / extendableMixin.js
Last active December 20, 2015 11:09
A simple mixin for making a Backbone.View extendable without worrying about overriding initialize or events.
/**
* View mixin - allows parent views to have their own events objects & initialize methods.
*
* Usage: On your parent view only:
* var MyView = Backbone.View.extend(ExtendableMixin).extend({ // ... })
*
* NOTICE: This completely breaks when using the Backbone-tools chrome extension. It modifies View.constructor.
*/
var ExtendableMixin = {
@ssafejava
ssafejava / gist:5640593
Last active December 17, 2015 16:39
Add 'routeNotFound' event to Backbone.History.
var oldLoadUrl = Backbone.History.prototype.loadUrl;
_.extend(Backbone.History.prototype, {
/**
* Override loadUrl & watch return value. Trigger event if no route was matched.
* @return {Boolean} True if a route was matched
*/
loadUrl : function() {
@ssafejava
ssafejava / gist:5544757
Last active December 17, 2015 03:39
Main.js with anchor link parsing
require([
'app',
'backbone',
// Application Router
'routers/ApplicationRouter'
],
function(app, Backbone, ApplicationRouter) {
// Define your master router on the application namespace and trigger all
@ssafejava
ssafejava / Backbone.layoutManager.Forms.compat.js
Last active December 17, 2015 02:48
Backbone.LayoutManager compatibility plugin for Backbone.Forms. Works with Backbone v1.0 and LayoutManager v0.9.
define([
'jquery',
'lodash',
'backbone',
'backbone-forms'
], function($, _, Backbone, BackboneForms) {
'use strict';
/**
* This class contains overrides & patches for the Backbone.Form class.