Skip to content

Instantly share code, notes, and snippets.

View brettinternet's full-sized avatar

Brett brettinternet

View GitHub Profile
@brettinternet
brettinternet / README.md
Created November 12, 2019 16:31
Event Delegation example

Event delegation is a technique involving adding event listeners to a parent element instead of adding them to the descendant elements. The listener will fire whenever the event is triggered on the descendant elements due to event bubbling up the DOM.

The benefits of this technique are:

  • Memory footprint goes down because only one single handler is needed on the parent element, rather than having to attach event handlers on each descendant.
  • There is no need to unbind the handler from elements that are removed and to bind the event for new elements.
Resources
@brettinternet
brettinternet / docker-compose.yml
Last active August 28, 2024 17:34
homelab setup
---
version: "3.7"
networks:
traefik_proxy:
name: traefik_proxy
driver: bridge
services:
traefik:
@brettinternet
brettinternet / copyStyles.js
Created December 18, 2018 22:46
Copy styles from current document to a new document
// from https://hackernoon.com/using-a-react-16-portal-to-do-something-cool-2a2d627b0202
function copyStyles(sourceDoc, targetDoc) {
Array.from(sourceDoc.styleSheets).forEach(styleSheet => {
if (styleSheet.cssRules) { // true for inline styles
const newStyleEl = sourceDoc.createElement('style');
Array.from(styleSheet.cssRules).forEach(cssRule => {
newStyleEl.appendChild(sourceDoc.createTextNode(cssRule.cssText));
});
@brettinternet
brettinternet / cssConcat.js
Created May 9, 2018 19:50
Concatination helper, which can merge class names together. Skips over falsey values.
function css() {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
var classes = [];
for (var _a = 0, args_1 = args; _a < args_1.length; _a++) {
var arg = args_1[_a];
if (arg) {
if (typeof arg === 'string') {
@brettinternet
brettinternet / App.js
Last active February 15, 2018 13:20
Fast API call with Create-react-app
import React, { Component } from 'react';
import { BrowserRouter, Route, NavLink, Link, Switch, Redirect } from 'react-router-dom';
import axios from 'axios';
/*
* HOME PAGE - SEARCH *
*/
class HomePage extends Component {
@brettinternet
brettinternet / ReactBindingApproaches.js
Created January 26, 2018 16:42 — forked from coryhouse/ReactBindingApproaches.js
React Binding Approaches
// Approach 1: Use React.createClass
var HelloWorld = React.createClass({
getInitialState() {
return { message: 'Hi' };
},
logMessage() {
// this magically works because React.createClass autobinds.
console.log(this.state.message);
},

Static fields (as class properties)

class UserSerializer(serializers.Serializer):
  email = serializers.EmailField()
  name = serializers.CharField()

Dynamic fields (overriding fields property)

class UserSerializer(serializers.Serializer):

API

URL Method Query Body Data returned Permissions Description
api/users/ GET List of users List users for requesting user's company
company=# filter by company Admins only

Servers

Name

@brettinternet
brettinternet / models.py
Last active April 11, 2023 19:14
phone number regex validator
from django.db import models
from django.contrib.auth.models import AbstractBaseUser
from django.core.validators import RegexValidator
# ...
class User(AbstractBaseUser):
# ...
phone_number = models.CharField(
max_length=16,
@brettinternet
brettinternet / time_del.py
Last active July 10, 2017 20:58 — forked from mattieb/time_del.py
Time various methods of removing a possibly-present item from a dict
#!/usr/bin/python3
import time
def new_d():
return {
1: 2, 3: 4, 5: 6, 7: 8, 9: 10,
11: 12, 13: 14, 15: 16, 17: 18, 19: 20
}