Skip to content

Instantly share code, notes, and snippets.

@redconfetti
redconfetti / example.js
Created April 24, 2014 23:15
Javascript Asynchronous Loop with clearing
// do something
var doSomething = function() {
var myLink = $('a#ajax-link');
if (myLink.length > 0){
myLink.hide();
stopDoSomethingLoop();
}
};
// do something every 300 milliseconds
@redconfetti
redconfetti / hash_decorator.rb
Created October 21, 2014 16:59
Camelize Keys
// Taken from https://github.com/rails-api/active_model_serializers/issues/398#issuecomment-26072287
class Hash
def camelize_keys!
update_keys!(:camelize, :lower)
end
def underscore_keys!
update_keys!(:underscore)
end
@redconfetti
redconfetti / my_custom_template.html
Created February 26, 2015 19:46
Creating a Template with Inputs via a Directive in AngularJS
(function(angular) {
'use strict';
angular.module('myapp.directives').directive('myCustomTemplate', function() {
return {
restrict: 'E',
templateUrl: 'shared/my_custom_template.html', // markup for template
scope: {
myList: '=', // Attribute used to pass an array of objects to render via the template: data-my-list="myList"
myConditional: '=', // Attribute used to specify some conditional state in template: data-my-conditional="false"
@redconfetti
redconfetti / ng-disabled-aria.html
Last active August 29, 2015 14:22
ngAria with ngDisabled receiving null and reporting as true
<!DOCTYPE html>
<html lang="en">
<head>
<title>Accessibility Test</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css"></link>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular-aria.js"></script>
</head>
<!--
@redconfetti
redconfetti / announcer.js
Created June 11, 2015 22:38
Screen Reader Announcer
/**
* Inserts hidden announcement into page for screen readers
* Expects 'cc-visuallyhidden' class to hide element from visual users
**/
var screenReaderAnnounce = function(message) {
// remove existing announcer
var existingAnnouncer = $window.document.getElementById('cc-sr-announcer');
if (document.contains(existingAnnouncer)) {
existingAnnouncer.parentNode.removeChild(existingAnnouncer);
}
@redconfetti
redconfetti / definition.rb
Last active December 21, 2015 05:19
Creating multiple associated records when using FactoryGirl
FactoryGirl.define do
factory :definition do
title "definition title"
body "definition body"
cached_slug "definition-title"
page 1
created_at "2011-01-01 08:30:00"
updated_at "2011-01-01 08:35:00"
factory :definition_object_relations_theory do
@redconfetti
redconfetti / powerball.rb
Created March 5, 2016 05:25
Powerball Numbers
picks = []
balls = (1..69).to_a
5.times do
picks << balls.sample
end
powerball = (1..26).to_a.sample
puts "picks: #{picks.join(', ')} - powerball: #{powerball}"[
@redconfetti
redconfetti / super-class-method.rb
Last active March 18, 2016 19:45
Using super with class and instance methods
# Testing to see how 'super' method works with class and instance methods
# when passing pre-declared variables into the methods where I am expecting them to be modified
# Results noted provided by jruby-1.7.19
class SuperClass
def self.add_something(somehash)
somehash['something'] += 1
end
def add_something(somehash)
somehash['something'] += 5
@redconfetti
redconfetti / constants-not-overridden.rb
Last active March 18, 2016 23:47
Constants in super classes not overridden by child classes
# It appears that a base class method refers to the constant value in that method
# instead of the constant value declared in child classes.
# You have to use a method to provide the expected behavior.
class BaseClass
COLUMNS = []
def self.shared_processor
child_processor(COLUMNS + ['column1'])
end

Relation between Javascript prototype and object

Example code demonstrating the relationship between the prototype and the objects instance values.

You can see that after explicitly setting the value of wheelCount to 4, it retains it's own value even after the prototype value is changed. The new prototype value of 18 applies to the new vehicle2 instance however.

Based on code from the post on "JavaScript constructors, prototypes, and the new keyword" by Pivotal Labs.