7 Patterns to Refactor JavaScript Applications: Form Objects

Note: This is part three (prev/next) of a seven part series that starts here.

Forms often have complex logic applied to them. In general, the logic breaks down into the following categories: validation, persistence, and feedback.

A Form Object can encapsulate all associated logic into a single object, keeping it focused, isolated, and easy to test. If you have a signup form that creates an account, an associated Form Object could handle the following logic:

  1. Make sure all fields that are required are present
  2. Make sure all values are valid (password is long enough, username isn’t taken, etc.)
  3. Send the form to the server so the user is saved to the database
  4. Provide success or error feedback to the user

Placing model validations on the Form Object instead of on a centralized model is perhaps counter-intuitive, since you may have to repeat these validations across multiple Form Objects that affect the same model. However, localizing validations to the form increases code readability (if you’re working on a form, you can just look at the Form Object) but also avoids cluttering your model with logic that is only used in form operations. Form Objects also give you fine-tuned control over the validations in their specific context and do not force you to guard against all scenarios on the model.

In fact, if we think about it on a higher level, we are off-loading the concept of a Model to the Form Objects and treating our "Model" objects like Data Access Objects (DAOs). If this is to be true, there has to be a bond of trust between the Model and the Form Object that what is being sent to the model is pure (i.e. well-formed, valid, complete). From an application architecture standpoint, this can be a really nice design pattern.

So, let's take a look at two examples. One of them demonstrating a full Form Object that covers all form operations and another demonstrating a Validation Object—an object describing validations that can be sequenced with other components.

Read More

Isolation with Linux Containers

Note: This is part two of a two part series that starts here.

In part one of this series, we built a simple echo server, and took steps to isolate the privileges, filesystem, allocated resources, and process space. The things we did isolated the echo server process from all the other processes on the host.

In this post, we’ll look at how Linux Containers provide an easier, more powerful alternative. Instead of isolating at the process level, we’ll isolate at the OS level.

Introducing Linux Containers

Docker is the hot new thing, but Linux containers (LXC) have been around since before Docker launched in March of 2013.

The Docker FAQ cites various differences between LXC and Docker. While Docker now utilizes libcontainer, it originally wrapped the LXC user tools. In summary, LXC provided a wrapper around Linux kernel technologies, while Docker essentially provided a wrapper around LXC.

This post look at the following technologies in the context of LXC:

  • Kernel namespaces
  • Chroots (using pivot_root)
  • uid_map and gid_map
  • cgroups
  • Virtual Ethernet
Read More
Learn about Engine Yard
Try Engine Yard for your Ruby or PHP Apps

A Smooth Transition to ECMAScript 6: Using New Features

In part one of this miniseries, we talked about the timeline for ES6 rollout, feature compatibility in existing environments and transpilers, and how to get ES6 set up in your build process.

Today, we’ll continue the conversation, looking at some of the easiest places to start using ES6 in a typical front-end Backbone + React project. Even if that's not your stack, read on! There's something for everyone here.

If you want to try out the examples, you can use a sandboxed ES6 environment at ES6 Fiddle.

New Features

Classes, Shorthand Methods, and Shorthand Properties

A lot of client-side JS code is object-oriented. If you're using Backbone, just about every Model, Collection, View, or Router you ever write will be a subclass of a core library Class. With ES6, extending these objects is a breeze. We can just call class MySubclass extends MyClass and we get object inheritance. We get access to a constructor method, and we can call super from within any method to apply the parent class's method of the same name. This prevents us from having to write things like:

Backbone.Collection.prototype.initialize.apply(this, arguments)
Read More

A Smooth Transition to ECMAScript 6: First Steps

I'm really excited about the newest version of JavaScript, ECMAScript 6 (ES6). But I'm also terrified. There's already so much to do between mentoring, contributing to open source, and working on the projects that pay the bills. When will I ever find the time to learn a whole new version of JavaScript?

As developers, it's our blessing and curse to always be learning. When I think about getting ready to adopt ES6, I feel some anxiety about the thought of having to figure out all of the new patterns and APIs it exposes.

In this miniseries, we'll look at some quick and easy ways to integrate ES6 into what you're working on today. Hopefully, by adding ES6 patterns into our coding practice a little at a time, we'll be able to avoid spending a weekend learning the new API when we could be playing outside.

Looking Ahead to ES6

ES6 will be the first update to JavaScript since ES5 was finalized in 2009. ES6 was originally slated to come out in 2013, but was then pushed back a couple more times, and is expected to be finalized this month, June 2015.

There's a lot of JavaScript in programming these days.

Read More

Cloud in My Coffee - Episode 3 with Gabe Monroy

After a month hiatus, it's time for your monthly dose of “Cloud in My Coffee”, the monthly video series from the Engine Yard Community Team. This video series features interviews with many of the people that make Engine Yard what it is every day. From the “C” level executives to those working the tickets on our Support Team, you’ll have the opportunity to get to know the Yardees around the world, all from the comfort of your seat, cup of coffee close at hand.

Read More

CHECK OUT OUR CURATED COLLECTIONS

Look through our specially curated posts to get focused, in-depth information on a single topic.