Skip to content

Instantly share code, notes, and snippets.

var app = require('express')()
, server = require('http').createServer(app)
, io = require('socket.io').listen(server);
server.listen(80);
var subscribers = {};
var publishMessage = function(channel, message) {
var sockets = subscribers[channel];
@jblashill
jblashill / promise.spread.js
Last active May 5, 2023 08:43
An extension to the Promise prototype to "spread" resolved data from Promise.all() to multiple function parameters
var Promise = require('es6-promise').Promise;
// idea borrowed from Q.spread
Promise.prototype.spread = function(fn) {
return this.then(function() {
if (!arguments || arguments.length === 0) {
return fn.apply();
}
return fn.apply(null, arguments[0]);
});
@jblashill
jblashill / promisify.js
Last active January 3, 2016 08:29
A function to easily adapt mongoose models to a ES6 Promises interface (using polyfill: https://github.com/jakearchibald/ES6-Promises)
var Promise = require('es6-promise').Promise;
var promisify = function (obj, func) {
return function () {
var args = Array.prototype.slice.call(arguments, 0);
return new Promise(function (resolve, reject) {
args.push(function (err, result) {
if (err) {
reject(err);
} else {
@jblashill
jblashill / backbone-bootstrap-row.js
Last active December 30, 2015 20:29
When working with backbone, marionette and twitter bootstrap, often I want to create a Marionette.Layout that maps a Twitter bootstrap row. Here's a simple function to generate a Layout from column sizes + names.
var bootstrapRow = function(cols, names) {
var snippets = _.map(cols, function(col) {
return "<div class='col-md-" + col + "'></div>";
});
var regions = {};
_.each(names, function(name, index) {
regions[name] = ">:nth-child(" + (index+1) + ")";
});
var GridLayout = Backbone.Marionette.Layout.extend({
className: "row",