Skip to content

Instantly share code, notes, and snippets.

@nnarhinen
nnarhinen / index.html
Created August 9, 2013 07:42
POST Form to new window and wait for postMessage
<html>
<head>
<title>Post form in a new window without losing handle to the window</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
$(function() {
$('form').on('submit', function(ev) {
var form = $(this);
form.attr('target', 'new-window');
@nnarhinen
nnarhinen / routes.clj
Created September 24, 2013 21:31
Use sessions with compojure
(ns server.routes
(:use compojure.core
server.views
[ring.middleware.json :only (wrap-json-response)]
ring.middleware.session
[ring.util.response :only (response)]
[hiccup.middleware :only (wrap-base-url)])
(:require [compojure.route :as route]
[compojure.handler :as handler]
[compojure.response :as response]))
@nnarhinen
nnarhinen / ajax-zip.js
Created February 6, 2014 06:26
Download (multiple) pdf files with ajax and add to a zip file in browser
var JSZip = require('jszip'),
Q = require('q');
var downloadFile = function(url) {
var defer = Q.defer();
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'arraybuffer';
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
@nnarhinen
nnarhinen / Gruntfile.js
Last active February 11, 2020 09:39
Support html5 pushState (or angular.js html5mode) in a yeoman (grunt-contrib-connect) application.
module.exports = function (grunt) {
// show elapsed time at the end
require('time-grunt')(grunt);
// load all grunt tasks
require('load-grunt-tasks')(grunt);
//MODIFIED: add require for connect-modewrite
var modRewrite = require('connect-modrewrite');
grunt.initConfig({
@nnarhinen
nnarhinen / README.md
Last active June 25, 2019 09:33
Rails-like console with express.js, bookshelf.js and node-repl-promised

Install node-repl-promised: npm install -g repl-promised

Use the repl to list all users

$ node-promised
> var app = require('./app');
undefined
> var Bookshelf = app.get('bookshelf');
undefined
@nnarhinen
nnarhinen / Products.tsx
Created February 20, 2019 06:30
React hooks + redux-hooks + redux-router-dom + immer-reducer
import React, { useEffect } from "react";
import { withRouter } from 'react-router-dom'
import { useProductSelectors, useProductActions } from "../redux/actions/products"
import Qs from 'querystring'
const Products = withRouter(({ location: { search }, history}) => {
const actions = useProductActions()
const [products] = useProductSelectors(selectors => [selectors.getProducts()])
import Raven from 'raven-js';
if (process.env.NODE_ENV === 'production') {
Raven.config('https://<foo>@app.getsentry.com/<bar>').install();
window.onunhandledrejection = function(data) {
Raven.captureException(data.reason);
};
}
const TeamForm = ({ handleSubmit, i18n, array }) => (
<form onSubmit={handleSubmit}>
{ /* Other form inputs */ }
<Field name="persons" component={renderRows(i18n)} array={array} />
{ /* Other form inputs */ }
</form>
);
@nnarhinen
nnarhinen / form-mixin.js
Created November 6, 2014 05:19
A react mixin helping with forms
'use strict';
var validator = require('shared/schemas/validator'),
_ = require('underscore');
module.exports = {
onPropertyChanged: function(property, newValue) {
var newObject = _.extend({}, this.props[this.objectPropertyPath], this.state[this.objectPropertyPath], _.isObject(property) ? property : _.object([[property, newValue]]));
newObject = this.transformObject(newObject);
var prms = {validationErrors: {}};
prms[this.objectPropertyPath] = newObject;
@nnarhinen
nnarhinen / app.js
Created December 30, 2015 13:19
i18n in redux react app
//omitted a lot
translations.fi().then(i18n => {
let initialState = {
locales: {
currentLocale: 'fi',
i18n
}
};
const store = am(createStore)(reducer, initialState);