Skip to content

Instantly share code, notes, and snippets.

@gurdotan
gurdotan / gist:1219199
Created September 15, 2011 13:19
git-diffall installation
cd ~
mkdir tools
cd tools
git clone https://github.com/thenigan/git-diffall.git
sudo ln -s ~/tools/git-diffall/git-diffall /usr/bin/git-diffall
@gurdotan
gurdotan / gist:1661805
Created January 23, 2012 08:40
SSH history autocomplete
complete -W "$(echo $(grep '^ssh ' ~/.bash_history | sort -u | sed 's/^ssh //'))" ssh
@gurdotan
gurdotan / gist:3596190
Created September 2, 2012 09:27
Git 3-way merge with meld
### Add the following to HOME/tools/bin/gitmerge
#!/bin/sh
meld $2 $1 &
sleep 0.5
meld $1 $3 &
sleep 0.5
meld $2 $4 $3
### Make it executable
@gurdotan
gurdotan / gist:3986841
Created October 31, 2012 12:42
Backbone View event bubbling
_.extend(Backbone.View.prototype, {
bubbleEventsTo : function(targetView) {
this.on("all", function() {
Backbone.View.prototype.trigger.apply(targetView, arguments);
});
return this;
})
(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)
@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:
//
@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:5910117
Created July 2, 2013 15:10
Ruby: Array to Hash
h = Hash[ *a.collect { |v| [ v, v ] }.flatten ]
@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;
}
});
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