Skip to content

Instantly share code, notes, and snippets.

View ianstormtaylor's full-sized avatar
🖖

Ian Storm Taylor ianstormtaylor

🖖
View GitHub Profile
@ianstormtaylor
ianstormtaylor / gist:2519665
Created April 28, 2012 14:57
Enhanced Backbone.Router route function to handle trailing slashes and query strings.
// Enhanced route function that automatically binds variant routes.
route : function (route, name, callback) {
var variants = [
route,
route + '/',
route + '?:querystring',
route + '/?:querystring'
];
@ianstormtaylor
ianstormtaylor / backbone_extensions.js
Created April 30, 2012 13:51 — forked from szimek/backbone_extensions.js
Mongo-style reduce for Backbone collections.
// Reduce collection using provided conditions
// e.g. FriendList.where({age: 30, gender: 'male'})
// Returns object of the same "class", so it's possible to chain methods
Backbone.Collection.prototype.where = function(conditions) {
return new this.constructor(_(conditions).reduce(function(memo, value, key) {
memo = _(memo).filter(function(model) {
return model.get(key) === value;
});
return memo;
}, this.models));
// javascripts/libs/underscore/underscore.min.js
// The actual underscore library.
...
// javascripts/libs/underscore.js
// A "module" that we require elsewhere.
@ianstormtaylor
ianstormtaylor / base.router.js
Created September 19, 2012 04:16
Enhanced Backbone Router to make a lot of things easier.
define([
'underscore',
'backbone'
],
function (_, Backbone) {
return Backbone.Router.extend({
// Variants of the routes to be automatically bound to. This way you
@ianstormtaylor
ianstormtaylor / kissmetrics.js
Last active December 14, 2015 06:49
Here's how to easily convert your KISSmetrics calls to Segment.io calls.
/**
* Identify
*
* KISSmetrics' identify and set calls turn into an analytics.identify call.
*/
_kmq.push(['identify', 'USER_ID_HERE']);
_kmq.push(['set', {
name : 'Achilles',
email : 'user@example.com'
@ianstormtaylor
ianstormtaylor / trackForm.js
Last active July 23, 2018 02:26
Tracking an email signup form, where you don't know the user's identity until the form is submitted, and the form is actually submitted to a 3rd-party mailing list so you don't have control over what happens when it's submitted.
// Grab the form using whichever DOM library you're using, or
// even just with `querySelector`.
var $form = $('#my-form');
// Call `trackForm` with your form, and a callback function
// instead of a normal `properties` object. The function will
// receive the form element as it's first argument.
analytics.trackForm($form, "Email Signup", function (form) {
// Find the email input and get it's value.
@ianstormtaylor
ianstormtaylor / trackLink.js
Last active December 15, 2015 00:18
How to track multiple links and add link-specific properties for the event by using a callback method.
@ianstormtaylor
ianstormtaylor / wordpress-tracking.php
Created March 17, 2013 23:00
Tracking additional events from the Segment.io WordPress library, directly in PHP.
$event = 'Event Name';
$properties = array(
'title' => single_post_title('', false)
);
Analytics::track($event, $properties);
@ianstormtaylor
ianstormtaylor / no-olark.css
Created May 7, 2013 18:47
A quick snippet to hide the Olark chat module on certain pages.
/**
* Hide Olark.
*/
#habla_beta_container_do_not_rely_on_div_classes_or_names {
display: none;
}
@ianstormtaylor
ianstormtaylor / view.md
Last active December 19, 2015 17:39
View spec.

View

View(model, el, options)

The View's constructor should take an optional model. It should also take an optional el so that the user can use a pre-made element instead of the View creating its own. If necessary, it should take additional options as a dictionary.

For convenience, add an el -> options overload if you add an options argument.

View.template

The View's template should live at .template if one exists so that it can be overriden if need be. The HTML for the view's template is also part of it's spec, so it should be well thought out. Use namespaced class names for child elements to avoid collisions (for example an .integration element's label would be .integration-label).