Skip to content

Instantly share code, notes, and snippets.

View lgsunnyvale's full-sized avatar

Josh Guo lgsunnyvale

  • Bay Area, California
View GitHub Profile
@lgsunnyvale
lgsunnyvale / gist:4401689
Created December 28, 2012 20:45
javascript in and filter_keys
var __in__ = function(elem, array){
array.forEach(function(item) {
if (item === elem) return true;
});
return false;
};
var __filter_keys = function(obj, filter) {
var result = {};
var extraKeys = $.grep(Object.keys(obj), function(el, i) {
@lgsunnyvale
lgsunnyvale / gist:4232713
Created December 7, 2012 11:32 — forked from fearphage/gist:1902781
Revert Backbone 0.9.1 from optimistic to realistic (read: classic) functionality
(function(CollectionPrototype, ModelPrototype, slice) {
var monkeyPatch = function(prototype, name) {
var method = prototype[name];
prototype[name] = function() {
var
args = slice.call(arguments)
,length = args.length
,options = length && length >= method.length && args[args.length - 1]
;
@lgsunnyvale
lgsunnyvale / gist:4232063
Created December 7, 2012 09:22 — forked from mattheworiordan/gist:1037984
Backbone patch to defer update method requests when new create requests are not complete on a model
(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)
@lgsunnyvale
lgsunnyvale / gist:4231729
Created December 7, 2012 08:14
compiled javascript code from coffee to show difference between thin arrow and fat arrow
(function() {
var A,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
A = (function() {
function A(msg) {
this.msg = msg;
this.fat = __bind(this.fat, this);
@lgsunnyvale
lgsunnyvale / gist:4229489
Created December 6, 2012 23:48 — forked from bruth/gist:2865951
Backbone Sync Queue
# Override `Backbone.ajax` to queue all requests.
# Cache Backbone ajax function, by default, just $.ajax
_ajax = Backbone.ajax
# Override Backbone.ajax to queue all requests to prevent
# lost updates.
Backbone.ajax = (options) ->
@ajax.queue options
@lgsunnyvale
lgsunnyvale / jquery-1.8.2.js
Created November 27, 2012 06:57
javascript isEmptyObject (from jQuery)
isEmptyObject: function( obj ) {
var name;
for ( name in obj ) {
return false;
}
return true;
},
@lgsunnyvale
lgsunnyvale / Default (Linux).sublime-keymap
Created November 27, 2012 03:56 — forked from coldnebo/Default (Linux).sublime-keymap
simple scripts to prettify your xml and json in sublime text 2
[
{ "keys": ["ctrl+shift+x"], "command": "tidy_xml" },
{ "keys": ["ctrl+shift+j"], "command": "prettify_json" }
]
@lgsunnyvale
lgsunnyvale / gist:4137802
Created November 24, 2012 00:16
dynamic tag generator js
var make = function() {
var v = {};
elems = ["a", "div", "span", "form", "h1", "h2", "h3", "h4"];
for (var i in elems) {
v[elems[i]] = make_handler(elems[i]);
}
return v;
}();
function make_handler(el) {
@lgsunnyvale
lgsunnyvale / gist:4134426
Created November 23, 2012 07:53
sexy pubsub
// Works in modern browsers + IE9, but Modernizr has a polyfill baked in for function.bind.
// Hat tip Paul Irish
var o = $( {} );
$.subscribe = o.on.bind(o);
$.unsubscribe = o.off.bind(o);
$.publish = o.trigger.bind(o);
// Usage
$(document.body).on( 'click', function() {
@lgsunnyvale
lgsunnyvale / jquery.ba-tinypubsub.js
Created November 23, 2012 06:56 — forked from cowboy/HEY-YOU.md
jQuery Tiny Pub/Sub: A really, really, REALLY tiny pub/sub implementation for jQuery.
/* jQuery Tiny Pub/Sub - v0.7 - 10/27/2011
* http://benalman.com/
* Copyright (c) 2011 "Cowboy" Ben Alman; Licensed MIT, GPL */
(function($) {
var o = $({});
$.subscribe = function() {
o.on.apply(o, arguments);