Skip to content

Instantly share code, notes, and snippets.

View macu's full-sized avatar

Matt Cudmore macu

  • Halifax, NS
View GitHub Profile
@macu
macu / nilmaps.go
Last active December 11, 2015 06:39
Go language tips
package main
import "fmt"
func main() {
var m map[int]string
fmt.Printf("m = %v; m == nil? %t\n", m, m == nil)
// it is safe to range over a nil map
for i, v := range m {
@macu
macu / anim.coffee
Last active December 12, 2015 03:09
Basic utility to simplify html5 animation. Exports the `start` function.
define ->
rAF = window["requestAnimationFrame"]
cAF = window["cancelAnimationFrame"]
# based on polyfill by Erik Möller,
# with fixes from Paul Irish and Tino Zijdel.
# https://gist.github.com/1579671
if !rAF or !cAF then do ->
for v in ['ms', 'moz', 'webkit', 'o']
@macu
macu / interrupt-confirm-exit.go
Created May 22, 2013 22:50
Example of listening for interrupt signal and confirming exit in the shell
package main
import (
"fmt"
"os"
"os/signal"
)
func main() {
@macu
macu / activate.js
Last active December 28, 2015 15:29
Add "active" to elements defining ownp attribute specifying a selector matching a parent element. This was written specifically to add "active" to nav elements for the current page in Bootstrap-based templates.
$(function(){
// loop all elements that specify an ownp attribute,
// and activate those in a parent matching the specified selector.
$("[ownp]").each(function(){
if ($(this).closest($(this).attr("ownp")).length) {
$(this).addClass("active");
}
});
@macu
macu / debug.coffee
Last active December 31, 2015 07:29
Minimal set of debug functions for use within jQuery plugins.
dopen = 0
debug = (o...) -> if settings.debug
console.debug o...
debugWarn = (o...) -> if settings.debug
console.warn o...
debugGroup = (o...) -> if settings.debug
console.group o...
dopen++
debugClose = -> if dopen > 0
console.groupEnd()
@macu
macu / form-includes.html
Created December 15, 2013 18:34
Form fields injection and sanitize number fields on submit.
<form data-includes="#anotherForm,[name=someField]" method="get">
<p>Fields from elsewhere in the document will be included on submit.</p>
<input type="submit" value="Submit!" />
</form>
<form id="anotherForm">
<input type="hidden" name="willBeIncluded" value=":)" />
<input type="number" name="willBeIncludedAndSanitized" min="1" data-sanitize="int" />
<input type="checkbox" name="willBeIncludedIfChecked" value="itWasChecked" />
</form>
@macu
macu / submit13.js
Created December 17, 2013 19:30
Prevent single-input forms from submitting when the user hits Enter, unless the input includes the `submit13` class.
// prevent all forms from submitting on pressing Enter in an input
// unless the input specifies the `submit13` class.
$("input").on("keypress", function(e) {
if (e.which == 13) {
// prevent all first, and trigger manually to bypass the quirk
// where browsers will submit single-input forms on pressing Enter.
// otherwise two submit events would be generated in quirk cases.
e.preventDefault();
if ($(this).is(".submit13")) {
$(this).closest("form").trigger("submit");
@macu
macu / inline-interface.go
Last active January 3, 2016 19:39
Some findings of interest about the Go language.
if f, ok := x.(interface { Foo() }); ok {
f.Foo()
}
@macu
macu / ember-naming.html
Created March 5, 2014 15:12
Ember naming conventions, compiled points (as of Ember v1.4.0)
<script type="text/x-handlebars">
The application template (outer scaffold) has no name, or has the name "application".
{{outlet}}
</script>
<script type="text/x-handlebars" data-template-name="dashboard">
This template embeds another view/controller/template combo with a hyphenated name.
{{render "users-list" visibleUsers}}
</script>
@macu
macu / ember-notes.md
Last active August 29, 2015 13:57
Compiled notes on Ember, things to be aware of

Ember Data

  • this.store.find('modelType'); always hits the server, because no ID is given, and there may be new records on the server. Use find in the application route, and all in other places to avoid polling the server every time.
  • this.store.find('modelType', model_id); only hits the server if the model with the given type and ID is not already in the cache.