Writing A Structural Directive in Angular 2

Or, how I wrote a customized version of ngFor

Posted on by Tero Parviainen

When building Angular 2 applications, we spend most of our time writing components. There are also other kinds of other kinds of directives we can define, but in my experience you end up needing to do that surprisingly rarely.

But recently I did end up in a situation where I needed a custom directive. I was using ngFor to render a collection of items, and I wanted to not keep track of items changing positions inside the collection. Instead I wanted a repeater directive that only adds and removes DOM elements but never actually moves them around to try to keep track of collection reorderings.

Read more →


Metabubbles: Making Generative Art with Angular 2

A tutorial using Angular 2, ES2015+, Babel, and Webpack

Posted on by Tero Parviainen

Most people talk about Angular 2 as a platform for writing business applications. But there's no rule that says this is the only thing it should be used for. If you're in the mood for something fun, how about making some generative visual art?

You see, the same characteristics that make Angular 2 a useful framework for business app development also make it a fun platform for pure experimentation and self-expression through code. In this tutorial we're going to do just that.

So let's have some fun with circles! While we're at it, we're also going to learn a whole lot about what Angular 2 development feels like and how it works with the popular Babel + Webpack tool stack.

Read more →



Refactoring Angular Apps to Component Style

Or, how to modernize your crufty AngularJS codebase one simple step at a time

Posted on by Tero Parviainen

Component-based directives are becoming increasingly popular in the Angular community. One reason for this is that Angular 2 will be all about components and people are preparing their 1.x code for the upgrade. Another reason is that componentization just makes apps easier to work with.

The thing is, even though people are talking about components a lot, most of the existing Angular code in the world is still not component-based. Components didn't become widespread until quite recently, and many of us are working with apps that have several years of history behind them. The foundations for these codebases were laid way before components were added to our collective toolbox.

In this article I present a case for refactoring existing Angular 1.x applications, whether large or small, towards component-based architectures. This is both to improve those apps in general and to prepare them for Angular 2.

I'll describe a catalog of specific refactorings that have been working for me when moving apps towards components. True to the spirit of refactoring, every change is broken down into small systematic steps. That is a requirement for being able to do this without breaking existing functionality, and without setting aside large amounts of time.

Read more →


Full-Stack Redux Tutorial

A Comprehensive Guide to Test-First Development with Redux, React, and Immutable

Posted on by Tero Parviainen

Update 2016-02-24: Updated react-router to 2.0.0. In tests, replaced use of deprecated setProps() with ReactDOM.render(). Also now using the react-addons-test-utils package so that no imports of 'react/addons' are needed anywhere.

Update 2015-11-06: Updated to the new Babel 6 release. The Babel packages we need to install are now a bit different, and an additional "babel" section is needed in the package.json in both projects.

Update 2015-10-09: Updated to React 0.14, React Router 1.0.0 RC3, and jsdom 6.x. The changes include:

  • Installing and using React and ReactDOM separately.
  • Using the PureRenderMixin from a separate NPM package.
  • Changing the way the routes are set up in index.jsx
  • Changing the way the current route's contents are populated into App.jsx
  • Removing the getDOMNode() calls in unit tests, as the test helpers now directly return the DOM nodes.

Update 2015-09-19: Clarified which version of the React Router is used. Various other small fixes and improvements. Thanks to Jesus Rodriguez and everyone who has been suggesting fixed in the comments!

Redux is one of the most exciting things happening in JavaScript at the moment. It stands out from the landscape of libraries and frameworks by getting so many things absolutely right: A simple, predictable state model. An emphasis on functional programming and immutable data. A tiny, focused API... What's not to like?

Redux is a very small library and learning all of its APIs is not very difficult. But for many people, it creates a paradigm shift: The tiny amount of building blocks and the self-imposed limitations of pure functions and immutable data may make one feel constrained. How exactly do you get things done?

This tutorial will guide you through building a full-stack Redux and Immutable-js application from scratch. We'll go through all the steps of constructing a Node+Redux backend and a React+Redux frontend for a real-world application, using test-first development. In our toolbox will also be ES6, Babel, Socket.io, Webpack, and Mocha. It's an intriguing stack, and you'll be up to speed with it in no time!

Read more →