Skip to content

Instantly share code, notes, and snippets.

😎
window.location.href="http://stackoverflow.com/search?q="+error.message

Brett brettinternet

😎
window.location.href="http://stackoverflow.com/search?q="+error.message
Block or report user

Report or block brettinternet

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@brettinternet
brettinternet / CatchOrCallback.js
Last active Dec 10, 2019
.Then Callback vs .Catch and how they're scoped
View CatchOrCallback.js
Promise.resolve()
.then(() => {
throw new Error('This will be handled in the next .then(, err), OR in a .catch'); // comment this line to see the next error handled
})
.then(() => {
throw new Error('This will only be handled in a .catch');
}, error => {
// an alternative to .catch with less scope
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then
// thus, with this method there's a block of code where errors could be left unhandled
@brettinternet
brettinternet / README.md
Created Nov 12, 2019
Event Delegation example
View README.md

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
View docker-compose.yml
---
version: "3.7"
networks:
traefik_proxy:
name: traefik_proxy
driver: bridge
services:
traefik:
@brettinternet
brettinternet / copyStyles.js
Created Dec 18, 2018
Copy styles from current document to a new document
View copyStyles.js
// 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
Concatination helper, which can merge class names together. Skips over falsey values.
View cssConcat.js
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 Feb 15, 2018
Fast API call with Create-react-app
View App.js
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 {
View ReactBindingApproaches.js
// 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);
},
View DRF_fields.md

Static fields (as class properties)

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

Dynamic fields (overriding fields property)

class UserSerializer(serializers.Serializer):
View api-tables.md

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 Nov 15, 2018
phone number regex validator
View models.py
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,
You can’t perform that action at this time.