Skip to content

Instantly share code, notes, and snippets.

var interval = setInterval(function() {
var expandPoints = $(".octicon.octicon-unfold");
expandPoints ? expandPoints.click() : clearInterval(interval);
}, 2000);
@gurdotan
gurdotan / gist:8899480
Created February 9, 2014 13:59
MongoDB corollary of SQL's "SELECT DISTINCT ... GROUP BY"
db.events.aggregate(
{'$group' : {_id : "$platform", devices: {"$addToSet" : "$device_id"} } },
{"$unwind" : "$devices" },
{"$group" : {_id : "$_id", count: {"$sum" : 1}}}
)
var BackboneViewExtensions = {
mixin : function(from) {
var to = this.prototype;
// we add those methods which exists on `from` but not on `to` to the latter
_.defaults(to, from);
// ...and we do the same for events and triggers
_.defaults(to.events, from.events);
// Define a router for the games sub-application
var GamesRouter = Backbone.Router.extend({
routes: {
"": "root",
"games": "index",
"games/:id": "showGame"
},
root : function() { /*...*/ },
index : function() { /*...*/ },
showGame : function(id) { /*...*/ }
RootApp.on("switchApp", function(appName, args) {
// Assuming the Root app has already instantiated all sub-apps
// and cached the instances by their names, fetch the app instance
var currentApp = RootApp.getSubappInstance(appName);
// If the app we're switching to is the currently active one, do nothing
if (RootApp.currentApp === currentApp) return;
// Stop the previously running app. Safe-guard with `if` in case
@gurdotan
gurdotan / gist:6775663
Created October 1, 2013 08:54
Backbone.Collection#fetchSingle - only one request in the air at a time
var MyCollection = Backbone.Collection.extend({
fetchSingle : function() {
if (this.currentFetch && this.currentFetch.state() === "pending") this.currentFetch.abort();
this.currentFetch = this.fetch.apply(this, _.toArray(arguments));
return this.currentFetch;
}
});
@gurdotan
gurdotan / gist:5910117
Created July 2, 2013 15:10
Ruby: Array to Hash
h = Hash[ *a.collect { |v| [ v, v ] }.flatten ]
@gurdotan
gurdotan / gist:5669730
Last active December 17, 2015 20:48
XHR2 file upload + jQuery deferred
var uploadFormAndFile = function(form, file) {
var formData = new FormData();
formData.append("email", form.email);
formData.append("name", form.name);
formData.append("thumbnail", file);
var uploadDeferred = $.Deferred(),
xhr = new XMLHttpRequest();
@gurdotan
gurdotan / gist:5253093
Created March 27, 2013 09:53
jQuery Deferred objects - example
// Based on http://www.intridea.com/blog/2011/2/8/fun-with-jquery-deferred
$.wait = function(time) {
var deferred = $.Deferred(function(dfd) {
setTimeout(dfd.resolve, time);
});
// Can return susbset of Deferred object
// without any state mutating functionality:
//
(function() {
Backbone.Model.prototype._save = Backbone.Model.prototype.save;
Backbone.Model.prototype.save = function(attrs, options) {
var that = this;
if (!options) { options = {}; }
if (this.savingNewRecord) {
// store or replace last PUT request with the latest version, we can safely replace old PUT requests with new ones
// but if there are callbacks from a previous PUT request, we need to make sure they are all called as well
_(['success','error']).each(function(event) {
// convert all callbacks to a single array of callbacks)