Skip to content

Instantly share code, notes, and snippets.

View suprememoocow's full-sized avatar

Andrew Newdigate suprememoocow

View GitHub Profile
var TodoList = app.LiveCollection.extend({
...
});
var LiveCollection = Backbone.Collection.extend({
...
_createEvent: function(body) {
...
// Look to see if this collection has any outstanding creates...
var idAttribute = this.model.prototype.idAttribute;
var unsaved = this.filter(function(model) {
return !model.id;
var fayeClient = new Faye.Client('/faye');
var LiveCollection = Backbone.Collection.extend({
constructor: function(models, options) {
Backbone.Collection.prototype.constructor.call(this, models, options);
this.subscription = fayeClient.subscribe(this.url, this._fayeEvent, this);
},
_fayeEvent: function(message) {
// This method will send realtime notifications to the faye clients
function notifyClients(method, model, url) {
bayeux.getClient().publish(url, {
method: method,
body: model.toJSON()
});
}
// Attach events to the mongoose schema. Since the default mongoose middleware makes it difficult to
// distinguish between a create and an update event, we use a small utility that helps us to do that.
var fayeServer = new faye.NodeAdapter({mount: '/faye', timeout: 45});
fayeServer.attach(httpServer);
// Start a server
var Faye = require('faye'),
server = new Faye.NodeAdapter({mount: '/'});
server.listen(8000);
// Create a client and subscribe to a channel
var client = new Faye.Client('http://localhost:8000/');
client.subscribe('/messages', function(message) { alert('Got a message: ' + message.text);
});
@suprememoocow
suprememoocow / winston-logrotate.js
Last active May 11, 2019 01:19
A function for reopening a winston File logging transport post logrotation on a HUP signal. To send a HUP to your node service, use the postrotate configuration option from logrotate. `postrotate kill -HUP ‘cat /var/run/mynodeservice.pid‘`
function reopenTransportOnHupSignal(fileTransport) {
process.on('SIGHUP', function() {
var fullname = path.join(fileTransport.dirname, fileTransport._getFile(false));
function reopen() {
if (fileTransport._stream) {
fileTransport._stream.end();
fileTransport._stream.destroySoon();
}
@suprememoocow
suprememoocow / intercept.js
Created May 29, 2012 09:53
AJAX timing interceptor: this class intercepts all AJAX calls and records the time taken for the HTTP request to complete. These timings are posted back to the server in batches, if there are any to send, about every two seconds. Tested in Firefox, Chrome
(function(XHR) {
"use strict";
var stats = [];
var timeoutId = null;
var open = XHR.prototype.open;
var send = XHR.prototype.send;
@suprememoocow
suprememoocow / (snippet) web.xml
Created January 6, 2012 13:37
PrecompressedStaticServlet: serves pre-compressed resource files from an Http Servlet container if they exist.
<web-app>
<!-- ... -->
<servlet>
<servlet-name>compressionHandlingDefaultServlet</servlet-name>
<servlet-class>com.barclayswealth.fsvoc.presentation.PrecompressedStaticServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
@suprememoocow
suprememoocow / DataBounce.js
Created December 16, 2011 15:12
DataBounce: server-side workaround for data: url problems in Internet Explorer. Full example at https://github.com/suprememoocow/databounce
var DataBounce = function(dataUrl, config) {
config = config || {};
var defaults = {
formName: "exportform",
filename: "",
exportFrameName: "exportFrame",
postUrl: "dataBounce"
};