Created
March 3, 2011 22:06
-
-
Save ahoward/853713 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Place your application-specific JavaScript functions and classes here | |
// This file is automatically included by javascript_include_tag :defaults | |
// | |
if(!window.jq && window.jQuery){ | |
var jq = jQuery; | |
}; | |
if(!window.App){ | |
window.App = {}; | |
}; | |
App.log = function(){ | |
try { | |
if(window.console){ | |
if(window.console.log){ | |
window.console.log.apply(window.console, arguments); | |
} | |
} | |
} catch(e) {} | |
}; | |
jq(function(){ | |
// ajax shortcuts | |
// | |
App.ajax = function(){ | |
var options = {}; | |
if(arguments.length == 1){ | |
var arg = arguments[0]; | |
if(typeof(arg)=='string'){ | |
options.url = arg; | |
} else { | |
options = arg; | |
} | |
} | |
if(arguments.length > 1){ | |
options.url = arguments[0]; | |
if(typeof(arguments[1])=='function'){ | |
options.success = arguments[1]; | |
options.data = arguments[2]; | |
} else { | |
options.data = arguments[1]; | |
options.success = arguments[2]; | |
} | |
} | |
var ajax = {}; | |
ajax.type = options.type || App.ajax.defaults.type; | |
ajax.url = options.url || App.ajax.defaults.url; | |
ajax.dataType = 'json'; | |
ajax.cache = false; | |
if(ajax.type == 'POST' || ajax.type == 'PUT' || ajax.type = 'DELETE'){ | |
ajax.data = jQuery.toJSON(options.data || {}); | |
} else { | |
ajax.data = (options.data || {}); | |
}; | |
ajax.contentType = (options.contentType || 'application/json; charset=utf-8'); | |
var result = App; | |
if(options.success){ | |
ajax.success = options.success; | |
} else { | |
if(typeof(ajax.async) == 'undefined'){ | |
ajax.async = false; | |
ajax.success = function(){ | |
var args = Array.prototype.slice.call(arguments); | |
result = args[0]; | |
App.ajax.results.push(result); | |
App.ajax.result = result; | |
}; | |
}; | |
}; | |
jQuery.ajax(ajax); | |
return(result); | |
}; | |
App.ajax.modes = ["options", "get", "head", "post", "put", "delete", "trace", "connect"]; | |
App.ajax.result = null; | |
App.ajax.results = []; | |
App.ajax.defaults = {}; | |
App.ajax.defaults.type = 'get'; | |
App.ajax.defaults.url = '/'; | |
// meta-program App.ajax.get(...), App.ajax.post(...) | |
// | |
for(var i = 0; i < App.ajax.modes.length; i++){ | |
(function(){ | |
var mode = App.ajax.modes[i]; | |
App.ajax[mode] = function(options){ | |
if(typeof(options) == 'string'){ | |
var opts = {'url' : options}; | |
options = opts; | |
} | |
options.type = mode.toUpperCase(); | |
App.ajax(options); | |
}; | |
})(); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great stuff Ara! So happy to see the code snippets from you in the form of gists after you closed drawohara.com. Thanks!