Skip to content

Instantly share code, notes, and snippets.

var config = {
"database": {
"connection": "mongodb://localhost/scrapService"
},
"cookieSecret": "EhAIj3NmtJ1sem5StHXV0Mk"
};
require("./user")
var express = require('express')
, mongoose = require('mongoose')
, User = mongoose.models["User"]
@alonbardavid
alonbardavid / suspend-computation.js
Created August 26, 2019 11:03
Suspending mobx computation
import { computed} from 'mobx'
const suspended = observable.box(false)
export function setSuspended(value) {
suspended.set(value)
}
export function suspendableComputed(
instance: any,
propertyName: PropertyKey,
descriptor: PropertyDescriptor,
@alonbardavid
alonbardavid / Controlled-components-snippet1
Last active September 2, 2019 11:42
Controlled components are awesome - and we don't talk about it enough - snippets
function myComponent(selector,label,value,onChange){
let element = document.querySelector(selector);
element.innerHTML=```
<div class="my-element">
<label>${label}</label>
<input></input>
</div>
```
let input = element.querySelector('input');
input.value = value;
function myComponent(selector,label,value,onChange){
let element = document.querySelector(selector);
element.innerHTML=```
<div class="my-element">
<label>${label}</label>
<input></input>
</div>
```
let input = element.querySelector('input');
input.value = value;
function myComponent(selector,label,value,onChange,validations = {}){
let element = document.querySelector(selector);
element.innerHTML=```
<div class="my-element">
<label>${label}</label>
<input></input>
</div>
```
let input = element.querySelector('input');
input.value = value;
function myComponent(selector,label,value,onChange,validations = {},format=null){
let element = document.querySelector(selector);
element.innerHTML=```
<div class="my-element">
<label>${label}</label>
<input></input>
</div>
```
let input = element.querySelector('input');
input.value = value;
test('when maxLength is 1, input should reject any keypress',()=>{
//assuming we have a div in our test environment with the id #test-app
const onChangeCalls = [];
myComponent("#test-app","my-label","A",{maxLength:1},(value)=>calls.push(value));
const input = document.querySelector("#test-app input");
//this test is a simplified version, to really simulate an input event, you'll need to
//change the target value, call more events and react like the browser.
input.dispatchEvent(new Event('keypress',{bubbles:true,cancelable:true,key:"B"}))
expect(calls.length).toEqual(0);
expect(input.value).toEqual("A");
function myComponent(selector,label,value,onChange,validations = {}){
let element = document.querySelector(selector);
element.innerHTML=```
<div class="my-element">
<label>${label}</label>
<input></input>
</div>
```
let input = element.querySelector('input');
input.value = value;
@alonbardavid
alonbardavid / Patterns-for-deriving-state gist1.js
Created October 2, 2019 12:24
Patterns-for-deriving-state gist1
class SortedList extends React.Component {
onFilterChange = (event)=>{
const filter = event.target.value;
const {sortKey} = this.state;
const filteredList = this.props.list.filter(item=>item.text.indexOf(filter)>=0)
this.setState({
filter,
sortedList:sortByColumn(filteredList,sortKey)
})
@alonbardavid
alonbardavid / code.js
Created October 2, 2019 12:25
Patterns for deriving state gist2
class SortedList extends React.Component {
onFilterChange = (event)=>{
const filter = event.target.value;
this.setState({
filter,
})
}
sortByKey = (column) =>{
this.setState({