Archive for the ‘ Web development ’ Category

Usefulness of the Underscore.js functional library

I usually ignore new frameworks or libraries until I see it referenced more than a dozen times and Underscore has reached that threshold. Recently I needed some more powerful libraries for working with collections and due to time constraints I decided not to write additional functions but instead give this library a try. Many of the functions in Underscore are already present in modern browsers and when these functions are found the library will defer to the native implementation–this was a huge selling point for me.

To keep this post brief I’ll get to some samples of usage:

Some of the functions that I found useful recently were:

This function

_.pluck(_productCollection.Items, "Id"), function (Id) {
return Id === product.Id;
})

Transforms the Items[] from this object into

_productCollection = {
    CriteriaType: "Products",
    Items: {
        Facility: null,
        FacilityId: null,
        Name: "",
        Id: ""
    }
}

An array of Id’s for additional processing

[Id,Id,Id]

This was useful to me when I had to compare the values of Ids against a new value to see if it was already contained in the initial object.

!_.any(_.pluck(productCollection.Items, "Id"), function (Id) {
    return Id === product.Id;
}))

Another useful area was removing an item in a collection with the following code:

productCollection.Items = _.reject(productCollection.Items, function (prod) {
    return prod.Id === productId;
});

This is definitely a useful library.

First deployment to Heroku

I’ve decided to use the Heroku Ruby on Rails hosting service due to it’s simplicity and integration with source control (git).  You can read more about it here.  There are many other services available, but I was attracted by the free account and also deployment is done in terminal like this:

 git push heroku master 

And creating a heroku account is done with this command:

heroku create tinyfireant

(Note: tinyfireant is the name of the site that you’re creating with Heroku)

Now one problem that I ran into was deployment, I kept getting this error:

git push heroku master

Permission denied (publickey).
fatal: The remote end hung up unexpectedly

After scouring the web for a few hours I finally gave up at 1am yesterday.  But I woke up this morning and couldn’t go to work without at least giving it another chance.  And wouldn’t you know it, 10 minutes later my site was deployed.  Thanks to this post here I discovered that I hadn’t uploaded my ssh keys (this is basically a certificate that faciliates terminal communication with Heroku without having to log in each time I interact with the service.)

So I regen’d my keys and uploaded them to heroku like this:

cd ~/.ssh

ssh-keygen -t rsa -C "myEmailAddressRegisteredWithHeroku.com"

heroku keys:add

git push heroku master

(Note: I didn’t enter a different file name, and left my passphrases empty.)

Then I saw this lovely message:

Counting objects: 73, done.
Delta compression using 2 threads.
Compressing objects: 100% (63/63), done.
Writing objects: 100% (73/73), 80.86 KiB, done.
Total 73 (delta 10), reused 0 (delta 0)

—–> Heroku receiving push
—–> Rails app detected
Compiled slug size is 80K
—–> Launching……… done
http://tinyfireant.heroku.com deployed to Heroku

To git@heroku.com:tinyfireant.git
* [new branch]      master -> master

Success! Now that I have deployment setup, I can start learning some more and deploying with 1 command line.  Hey Microsoft, you listening? :0)