Skip to content

Instantly share code, notes, and snippets.

View JonMidhir's full-sized avatar

John Hope JonMidhir

View GitHub Profile
@JonMidhir
JonMidhir / Margin-hack.css
Created October 6, 2012 16:32
Margin:auto to Push/Pull and item
/* CSS Hack to push/pull a fixed width element left/right without floating.
Probably common knowledge but it's new to me. */
/* Margin: auto is often used to center one fixed-width element within another, like so */
#element1 {
width: 980px;
}
#child1 {
@JonMidhir
JonMidhir / validations.rb
Created November 5, 2012 12:44
Does validates :uniqueness get called even if a field has not changed?
# Does validates :uniqueness get called even if a field has not changed?
# Isn't this a performance issue?
validates :name, :schedule_id, :uniqueness => true
# My experience that it does. So isn't it almost always necessary to make sure a change has taken
# place before running the validation? As every field being checked for uniqueness requires a
# database hit.
# This would be better:
@JonMidhir
JonMidhir / Opacity-mixin.scss
Created February 26, 2013 18:36
I use this mixin to provide cross-browser support for Opacity in SASS
@mixin transparency($opacity, $microsoft_friendly_stupidly_unneccessary_percentage_value) {
zoom: 1;
filter: alpha(opacity=$microsoft_friendly_stupidly_unneccessary_percentage_value);
opacity: $opacity;
}
@JonMidhir
JonMidhir / array_iteration_fun.coffee
Last active December 16, 2015 02:28
Array iteration fun in CoffeeScript
# Say you want to iterate over a variable that might be an array or null/undefined,
# you can do this:
for order in orders or []
# Do something with orders
# This simply iterates over the empty array if book is null or undefined. Handy if
# you have an object like this:
customers = {
@JonMidhir
JonMidhir / imagePreload.js
Last active December 20, 2015 20:09
Here's a handy little plugin for jQuery to preload images on a webpage. Planning to use it on midhirrecords.com
$ = jQuery
// Takes an array of strings
$.preloadImages = function(images) {
$(images).each(function() {
$('<img />')[0].src = this;
});
}
// Use it anytime after the document loads
class ShiftDock.Views.RotasIndex extends Backbone.View
# ...
initialize: ->
@collection.on 'fetched', @render, this
render: ->
# ...
# ...
@JonMidhir
JonMidhir / rotas_router.js.coffee
Created September 23, 2013 11:09
If you're not destroying your collection over the lifetime of the router you can do it this way. Benefit is that the view isn't re-initialized every time you hit a router action, instead you can focus on triggering the right events on it.
class ShiftDock.Routers.Rotas extends Backbone.Router
routes:
'': 'index'
initialize: ->
@collection = new ShiftDock.Collections.Rotas()
@collection.view = new ShiftDock.Views.RotasIndex(collection: @collection, el: '#container')
index: (year, week) ->
@collection.fetch
# 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,
@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()}]")
def as_json
super(methods: [:balance, :available])
end