Skip to content

Instantly share code, notes, and snippets.

@jamesarosen
jamesarosen / jquery-form-ajax.js
Created August 8, 2012 19:53
jQuery function that causes a form to submit via AJAX
function buildSubmitHandler(ajaxOptions) {
function submitViaAJAX(event) {
var data = $.extend( {}, ajaxOptions.data, $form.serialize() ),
options = $.extend({}, {
url: $form.attr('action'),
type: $form.attr('method'),
}, ajaxOptions, { data: data });
$.ajax(options)
@jamesarosen
jamesarosen / conditioner_usage.js
Created June 9, 2012 01:40
Conditioner: a jQuery plugin for building n-ary conditional statements.
// This is the proposed API for a jQuery plugin that will let you build
// forms that look something like this example from iTunes:
// https://img.skitch.com/20120609-nk4hfrjm7ybnfmw7drhm64eq8a.png
// Each row consists of a "field," an "operator," and possibly a "target value."
$('form') .conditioner({
name: Conditioner.STRING,
age: Conditioner.NUMBER,
price: Conditioner.NUMBER,
@jamesarosen
jamesarosen / app.js
Created May 2, 2012 23:37
Closure for each app installation
defineApp({
defaultSheet: 'loading',
requests: {
loadFoos: function(id) {
return { url: fmt('/tickets/%@/foos', id) };
}
},
@jamesarosen
jamesarosen / nginx.conf
Created April 28, 2012 18:58
Rails + EM + nginx Proxy
# When a proxy request comes in, send it to Rails for authorization:
location /api/proxy {
proxy_pass http://rails;
}
# Rails will send an X-Accel-Redirect if the proxy request is valid,
# attaching any domain information the proxy needs so it doesn't have
# to do database lookups:
location /proxy {
internal;
@jamesarosen
jamesarosen / 01-foo-details-view.js
Created April 27, 2012 18:01
Ember: Task Link States
Tasks.FooDetailsView = Em.View.extend({
templateName: 'tasks/details/foo'
})
@jamesarosen
jamesarosen / pagination_controls.handlebars
Created April 11, 2012 01:35
An Ember.js Handlebars template that has trouble rendering in IE9
<ul>
<li {{bindAttr class="controller.isFirstPage:disabled"}}>
{{#view Zendesk.Pagination.LinkView action="goToFirstPage"}}
«
{{/view}}
</li>
<li {{bindAttr class="controller.isFirstPage:disabled"}}>
{{#view Zendesk.Pagination.LinkView action="previousPage"}}
{{/view}}
@jamesarosen
jamesarosen / root_view.hdbs
Created February 7, 2012 01:45
Ember re-render stack trace
{{#view "Zendesk.RootView" id="wrapper" classBinding="Zendesk.section Zendesk.idle"}}
<div id="loading" {{bindAttr class="Zendesk.stillLoading"}}></div>
(a huge number of other views...)
{{/view}}
@jamesarosen
jamesarosen / 1_template_container_view.js
Created February 7, 2012 00:43
template containers in Ember
// A mixin for `Em.ContainerView` that allows an initial render from
// a template, followed by further additions by mutating childViews.
// Note that the template should *only* contain calls to
// `{{childView}}`
App.Mixins.TemplateContainerView = Em.Mixin.create({
render: function() {
if (!this.get('_renderedFromTemplate')) {
Em.View.prototype.render.apply(this, arguments);
this.set('_renderedFromTemplate', true);
@jamesarosen
jamesarosen / redcarpet_template_handler.rb
Created February 2, 2012 00:37
RedCarpet Rails Template Handler
# This version works in Rails 3.2
require 'redcarpet'
class ActionView::Template
class Redcarpet
class <<self
def renderer
@renderer ||= ::Redcarpet::Markdown.new(
::Redcarpet::Render::HTML,
:autolink => true,
@jamesarosen
jamesarosen / cldr.plurals.js
Created January 19, 2012 17:26
CLDR Pluralization Rules in Javascript
(function(globals) {
// CLDR Pluralization Data
// see http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html
// The six plural forms. Not all languages use all six forms.
var Z = 'zero',
O = 'one',
T = 'two',
F = 'few',