# Setting Up CouchDB on Ubuntu in Azure

1. Create a new Ubuntu 14.04 LTS virtual machine.

2. Add an endpoint for port 5984.

3. SSH to your newly created machine.

 $sudo apt-get update  5. Install CouchDB. $ sudo apt-get install couchdb

6. Ensure that CouchDB is working.

 $curl -X GET http://localhost:5984 {"couchdb":"Welcome", "uuid":"80543311a1c6ed1de302adfc970a6d37", "version":"1.5.0", "vendor":{"name":"Ubuntu", "version":"14.04"}}  7. By default CouchDB gets installed and works only on localhost. We will need to edit CouchDB’s configuration file to allow it to work on public ports. $ sudo vim /etc/couchdb/default.ini


Find the line that looks like bind_address = 127.0.0.1. Change that to bind_address = 0.0.0.0.

8. Restart CouchDB.

### The controller (PointCtrl.js)

Our PointCtrl is quite small, especially since most of the work is being handled by the point service. That was why we wrote the service in the first place.

The test for the controller is similarly simple, once you figure out the test setup steps.

Again, the trick to these tests is understanding the beforeEach() functions. The first one loads the module. The second loads the $controller factory. You will notice that we create a mock point object and a mock $scope object. I have discussed mocking JavaScript objects before, and I follow the same pattern here. Since there is no type system in JavaScript, we only care that the object we create has the same functions and properties as what we use.

How much of a mock do you need? You need as much of the object surface area as the controller has access. For $scope, an object literal ({}) will do, and there is no reason for anything more complicated than this. The point object is a little more complicated. First, it needs to be a function, so we can call point(). Second, the controller needs access to the distanceTo() function. We will create a stub for this function and return a random value between 0 and 100. ### The final result Running all of tests in the browser, we should see the following. ### The complete series # Angular Part 4: Creating a Service Our last project covered dependency injection in an Angular application. Now, let’s show how a service works. An Angular service is a way to encapsulate business logic so that it can be consumed by controllers and other services. Like any well-written program, functionality that goes together should be put together in small, reusable, testable components. Speaking of testing, that’s going to be the next post, so stay tuned. ### Demonstration of application The application should work similar to the following example. ### Our new service We will encapsulate the point object into an Angular service. This service will be called “point”, and it will have no dependencies. You will notice that this block of code looks almost identical to the point() function from our previous post. By giving our service a unique name, we can now inject it into our controllers as needed. Adding a service makes our controller much simpler than the previous post. Additionally, our point is now a focused, reusable block of code that can be used anywhere within our application. It is important to note that all Angular services are lazy singletons. Specifically, they are only created when asked for, and only one instance of the function will ever be created. This is great for your application’s memory profile. ### The complete series # Angular Part 3: Dependency Injection In my previous post, I showed how a controller is built in Angular. The basic syntax for creating a controller looks like the following. This syntax for creating a JavaScript module is incredibly powerful. By letting Angular handle all of the dependencies, and by treating the scope as yet another object to be injected, the developer needn’t ever worry about three of the most difficult concepts in JavaScript: specifically, closures, new, and this. ### The Traditional JavaScript Object For a refresher, here’s the traditional way to create an object in JavaScript. We create a new point in the following manner. In the above class, this is always the functional enclosure that defines the class. This means that this acts the same as we would expect it in C# or Java. However, it wouldn’t take much, just a simple callback, to change the result. Imagine a more complex function that needs to be non-blocking. Yes, there are ways to get around the above, specifically by learning how bind(this) works, but, in my experience, this is a place where many developers tend to not know what they are doing. Unfortunately, too many JavaScript “developers” are frequently C# or Java developers who have been asked to add functionality to a web site. They then write mediocre, fragile JavaScript. ### An Alternate JavaScript Object Instead, let’s examine an alternate way to writing our Point class. In this example, we are creating an object literal and attaching functions to this new object. No longer do we have to worry about immediate functions, the (function () {})() syntax, this, or even creating a new object. To create a new point in this manner, we simply do the following. ### Angular Objects Angular gets around of all of this (Ha! Ha! Puns!) by injecting the scope object. Anything you wish to expose gets attached to the scope object. Also, the developer never needs to create new objects. The Angular framework does all of that work for you. Let’s create a simple form for displaying the distance between two points. Our controller will look something like the following. Together, the above HTML and JavaScript should give us something like this. As mentioned previously, anything that gets attached to $scope is publicly accessible. The squareError() function is not available publicly. There is no binding trick that the developer can use to attach the function to any Angular directive.

Finally, let’s look at the format of the controller definition.

The String is the name of the controller. The Array is the names of the dependencies with the last element of the array being the definition of the controller. Dependencies that start with a $ are built-ins for Angular — there are several. ### Conclusion I recently watched Douglas Crockford talk about writing JavaScript without using new or this. At first I was quite taken aback. How the hell am I supposed to write JS without objects? As you can see, we can still create plenty of objects, those objects have attributes, attach functions to those objects, and even have private function encapsulation without ever using new or this. ### The complete series # Angular Part 2: Moving to a Controller In Part 1, we showed how easy it is to add functionality to a website with AngularJS, and compared that to how much work would be necessary to do the same thing using jQuery alone. We did have a little bit of logic in our views, though, and that’s not ideal. That logic was testing for specific values for our form fields. In my opinion, even something as simple as a test for equality is view logic, and we should do what we can to move that out of the view. Ideally, we want something that looks more like this. Moving that logic is not difficult. In Angular, this kind of thing is handled by the controller. Before we can write a controller, we first need a module. Defining a module takes the signature angular.module(String, Array). The string is the name of the module, and the array is the list of dependencies required by the module. Below, we define a module named myApp, followed by an array of dependencies. In this case, we do not have any dependencies, so we just have an empty array. Now that we’ve defined our module, let’s define our controller. We get our module from the Angular registry and add a controller to it. Defining a controller takes the signature module.controller(String, Array). The array is the list of dependencies followed by the definition of the controller. Our controller does have a $scope dependencies. $scope is Angular’s way of not having to deal with JavaScript’s this object. Anything that we want to expose, we add to the $scope object.

Let’s take look at updated HTML.

We added our module’s name to the ng-app="myApp" directive. We added our controller as part of the ng-controller="ColorFormCtrl" directive. We then moved all of our page logic to a controller.

# Angular Part 1: Show and Hide

This week, I have been diving headfirst into AngularJS. I am amazed at how much one can accomplish with very little code. In this example, I want to show and hide various parts of a form based on what selections the user makes elsewhere. That’s a pretty simple thing to do with jQuery. It’s even easier with AngularJS.

First, let’s show our base markup. This is the bare minimum to get our page to work.

The above HTML will render as follows.

If we already know jQuery, we know that we need to add a little more to our markup to make it play nice with jQuery.

1. Add an ID to our select box.
2. Add an ID to our wrapper div tag for our “Other” input.
3. Hide the “Other” form field by default.
4. Write a little bit of jQuery.

That’s really not too bad. Let’s however, add a few more requirements.

1. The Favorite Color option needs to be required.
2. The Other field needs to be required if Other is selected.
3. Don’t let the user submit an invalid form.

HTML 5 gives us some of this, but we still run into walls. We’re going to have to add a disabled attribute to the button. We’re going to have to validate our form with another library (or roll our own). We need to add required to the select box. We need to add required to our text box, but only if “Other” is selected.

The program flow should look like this.

This is the default state of the form.

User selects an option that is not “Other”. Everything is good. Enable the submit button.

User select “Other”. Show a text box and disable the submit button.

User enters a value in the text box. Everything is good again. Enable the submit button.

Again, none of this is particularly difficult, but you can imagine how all of this blows up when a page gets to be of a certain complexity. Let’s see what AngularJS has to offer.

1. Add ng-model tags to the form fields that we need to expose to angular. That’s both of them.
2. Add ng-required to any field that is dynamically required.
3. Add ng-disabled to any field that is dynamically disabled.
4. Wire it all together.

Let’s look at our AngularJS markup

Let’s compare our jQuery markup with our AngularJS markup.

1. The body tag now has an ng-app="" attribute. This is what tells Angular that it should work.
2. We have added a name="form" attribute to our form. By giving the form a name, it can be accessed by Angular. This access can be seen on the submit button, with the form.\$invalid variable.
3. We have added ng-model="..." to both of our input fields. This exposes the form fields to Angular.
4. We have added a required HTML5 attribute to our select.
5. We have added ng-show="favoriteColor === 'Other'" to our div tag that wraps our text box. This means that the field will only be shown if the expression is truthy.
6. We added an ng-required attribute to our text box. This means the field will be required if the expression is truthy.
7. Finally, we added an ng-disabled attribute to our button. This means that the button will be disabled if the expression is truthy.

Now, let’s show the JavaScript we need to write to make all this magic happen.

Yep. We’re done. Zero lines of JavaScript. Yeah, I could get used to this.

# Oops! How to Permanently Delete a File From Git

I accidentally added an uninitialized (appended) SQL Server backup file, a *.bak, to source control and pushed it. Now, no one can fetch a clean copy of the source because of out-of-memory errors. How do I fix this?

This will take a while to run, as it goes through every commit in your project and deletes the file. When you’re done, you’ll need to push the file to your remote.

You’ll need the --force flag because this is not a fast-forward commit.

# Getting Started With MathJax

I’ve spent the past few days getting myself reacquainted with TeX and familiarizing myself with MathJax. As it turns out, it is quite easy to get MathJax working in Octopress.

1. Open source/_includes/custom/head.html.
2. Add the a JavaScript source: //cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-MML-AM_HTMLorMML.
4. In your Gemfile, make sure you are using at least RDiscount version 2.1. gem 'rdiscount', '~> 2.1'
5. In your _config.yml file, disable automatic superscripts by adding - no_superscript to your RDiscount configuration.

The last two are quite important. Without those two steps, it looks like RDiscount will turn x^2 into x<sup>2</sup>. This will get displayed as x2. That will break the MathJax processor.

Let’s start by demonstrating a simple inline equation, like $$y = mx + c$$. This is simple enough by including the inline text: \$$y = mx + c \$$.

To enter full equations, like the one below, use the following syntax.

The above code will render as the following equation.

$$x = \dfrac{ -b \pm \sqrt{ b^2 - 4ac } }{ 2a }$$

Happy mathing!

# Mocking a JavaScript Constructor

JavaScript, and dynamic languages in general, make it extremely easy to mock out functions. This greatly reduces the need for all the extra programming junk (interfaces, adapters, wrappers, factories, etc.) that gets in the way of us solving our problems. We add tons of extra layers in the name of test isolation and object independence that plainly isn’t necessary in the dynamic languages world.

We’ve defined a very simple class, and it has all of the parts that we need to demonstrate how mocking works.

First, let’s define some simple tests. I’m using Jasmine and Node. (See jasmine-node for a command line version of Jasmine.) These tests fully describe the basic functionality of the class.

### Mocking an instance function.

The distanceTo() function exists on the instance of Point. It’s incredibly easy to mock out. Functions are first-class members in JavaScript, so we can simply redefine the function in the unit test. In this example, we simply redefine the function to return 0.

### Mocking a class function.

The origin() function is a class function that returns the point {0, 0}. Let’s suppose we need to define a different origin. That’s easy to do!

In this case, we need to store the original definition of origin() in the temp variable. In beforeEach(), we will override the origin() function. In afterEach(), we will restore it to its former glory.

### Mocking the class constructor.

Of the three, mocking the constructor takes a little more insight, but the activity is functionally the same. We are going to create a new class and copy all of the necessary functionality to the new class. We do this by copying over the prototype of the original point. We also need to explicitly copy over the origin() function, since class functions are not part of the prototype.

The pattern is the same. Create a copy. Redefine the function. (The constructor is just another function, just treated a little differently.) Do your tests. Restore the original definition.

I hope this clears up some of the issues around mocking JavaScript objects. This can be done easily with the language and require no additional 3rd party libraries.