design patterns Chained callback pattern in Javascript Imagine we have four methods a(next), b(next), c(next), d() where the first three accept a callback. We want to execut these methods in order, so that a completes and calls b, b completes and calls c, and c completes and calls
javascript Function wrapping with Javascript Decorators One of the best uses for ES7/Javascript 2017 decorators is adding additional functionality to a function. This article will discuss the basics of decorators and show some patterns that can be used to help write cleaner code. Note: ES7/Javascript 2017 decorators are
javascript Testing async/await with Babel and Mocha I was curious about testing ES6/7 code transpiled with Babel. Getting it to work is fairly straightforward. Here's what I found... Setup The documentation for Babel tells you to setup an NPM script to run mocha tests as such: { "scripts": { "
javascript Gettings started with Babel and Node I've been using Babel recently to perform transpiling of Node.js code. I've previously used Traceur and it always left a bit to be desired. The big winner now, is the support of ES7 code such as async/await. This guide will show a
javascript Replacing jQuery enrichment with Backbone on a server-side rendered website. My task was adding a newsletter signup form to a website. Simple enough task. The site is rendered on the server-side using Node.js, Express and Handlebars. Performing a full page refresh to post a bit of data on a single-page marketing site seemed
javascript Super class access in JavaScript The traditional way to access overridden functions in JavaScript is a bit kludgy. For exaxmple, given you have a child class Child that inherits from a super class Parent you would do something like... Child.prototype.validate = function() { // call base class method Parent.prototype.
javascript Promise + Callback pattern for JavaScript As I've been working with Promises more, I've found a need to migrate existing code from callback passing syntax to returned promise syntax. Since the Node ecosystem favors the callback passing syntax, it is wise to make your library support both callbacks and Promises.
javascript JavaScript ToDictionary Anyone familiar with Linq's ToDictionary method may be looking for a similar technique in JavaScript. The cool thing about JavaScript is that first-order properties (properties attached to the first prototype of the instance) are usually implemented in a "dicationary-like" way. Thus we
javascript Simplify Promise chains with Arrow Functions Arrow Functions are a new feature of JavaScript slated for arrival in ES6. They provide new syntax for creating function expressions. In the JavaScript world, we frequetly create function expressions when we make anonymous functions or define a function. Arrow Functions give us another
javascript Check if something is an Array, Object, or Function in JavaScript There are a few techniques for this, but here's what I always fall back. Writing this down for posterity. function isArray(sut) { return Object.prototype.toString.call(sut) === '[object Array]'; } function isObject(sut) { return Object.prototype.toString.call(sut) === '[object Object]
javascript Understanding exceptions in Promises Many articles exist on how promises work. This article attempts to explain error propgation when using promises. Lets start with a simple function that throws an exception. function boom() { throw 'BOOM!'; } What happens when we execute the boom function as a promise using
javascript Generating a UUID in Javascript Say you want to generate an RFC4122 version 4 compliant UUID in Javascript. Here are a few techniques. I created a test harness to test various generation methods. Test Harness This harness allows testing performance, validity, and collisions. It works by accepting a generator
javascript JSLint with Grunt Grunt is fantastic for automating linting tasks and enforcing coding standards by making it extremely easy to follow them! JSLint has many different options that can be configured. In order to get JSLint to run you can use the grunt-jslint npm package. This pacakge
javascript Getting started with Grunt Grunt is a "task runner". But what does that mean? Well grunt simplifies things by allowing you to perform a number of tasks with a single command. This means that you, and your team, can easily enforce coding standards, execute tests, or
javascript Load a Handlebars template from a file Handlebars templates are pretty awesome to work with. The quickest way to get started is to put a template in a script block and use the "text/x-handlebars-template" content-type. <script id="badtemplate" type="text/x-handlebars-template"> <
javascript Attach view to existing element in Backbone You can attach a view to an existing element in Backbone.js. By default Backbone will create a new div for your view and is accessible in the el and $el properties. To attach to an existing element you can: Set the el property