Skip to content

Instantly share code, notes, and snippets.

View JonMidhir's full-sized avatar

John Hope JonMidhir

View GitHub Profile
// Set up the current user
app.services.factory('CurrentUser', function(User) {
var bootstrapCurrentUser = function() {
var data = JSON.parse(currentUser);
var user = new User(data);
return user;
}
return bootstrapCurrentUser() || User.me();
@JonMidhir
JonMidhir / Backbone_turbolinks_setup_2.coffee
Last active August 29, 2015 14:04
Demonstrating an example setup of a Backbone app in a Rails project with Turbolinks, from https://medium.com/p/edf4849ddb73
@JonMidhir
JonMidhir / bookmark_me.coffee
Last active August 29, 2015 14:03
Cross-browser, cross-platform JavaScript function to add the current page to the browser bookmarks
addToBookmarks = ->
e.preventDefault()
title = document.title
url = document.location.href
# IE
if window.external?.AddFavourite
window.external.AddFavorite(url, title)
@JonMidhir
JonMidhir / backbone_turbolinks.coffee
Created June 28, 2014 13:04
From Part 1 of Rails: Building a single-page Backbone app without sacrificing Turbolinks. https://medium.com/p/3e22841cf834
@JonMidhir
JonMidhir / backbone-trackjs.coffee
Last active August 29, 2015 14:03 — forked from toddhgardner/backbone-trackjs.js
Tracking Backbone errors with track:js, ported to CoffeeScript
# OPTION 1:
# Automatically wrap everything
do ->
'use strict'
return unless window.trackJs?
for klass in ['View', 'Model', 'Collection', 'Router']
old = Backbone[klass]
@JonMidhir
JonMidhir / collection_fetch_abort.coffee
Created April 11, 2014 13:15
Aborting a Backbone Collection fetch request
# Collection.fetch() returns a Xhr that you can store in a variable.
# The request can be aborted if the Xhr readystate is 1,2 or 3.
#
# Note: If readystate == 3 the request has already been sent and the
# server will continue to process it, the response will be ignored.
class App.Routers.Posts extends Backbone.Router
routes:
'': 'index'
':id': 'show'
@JonMidhir
JonMidhir / chronic_in_shiftdock.rb
Created March 28, 2014 12:31
Setting time attributes using Chronic to parse human text input.
class Shift < ActiveRecord::Base
validates :starts_at, presence: true
def parsable_start_time
starts_at.strftime("%-I.%M%P") if starts_at
end
def parsable_start_time=(time_string)
self.starts_at = Chronic.parse(time_string)
end
def as_json
super(methods: [:balance, :available])
end
@JonMidhir
JonMidhir / dependent_selected.coffee
Created October 18, 2013 15:01
Here's a simple way to make multiple select fields dependent on one another. When a value is selected in one it is disabled in the others and cannot be selected.
$.fn.dependentSelect = ->
$(@).addClass('dependent')
@each ->
$(@).data('pre', $(@).val())
@disableOthers = ->
$('select.dependent')
.not($(@))
.find("option[value=#{$(@).val()}]")
# Assume response JSON for `book` looks like this:
# {
# id: 1,
# pages: [{
# id: 1,
# chapter: 1,
# text: 'It was the best of times, '
# }, {
# id: 2,
# chapter: 1,