HTML5

I was finally able spend some time with HTML5 localStorage, one of the new specs that I was confused and excited about. I thought it would be interesting to use it for data storage for a web app prototype I was working on, so I dove in and learned more about it.

My plan was to create and store Javascript objects in localStorage so I could recall them later throughout the app. I figured I’d be making objects and storing them as hashes. Much to my dismay HTML5 localStorage only stores key/value combinations, so instead of hashes I was forced to stringify the objects with the JSON.stringify function .

// Object model
function guitar(make,model,year,pickupConfig) {
  this.make = make;
  this.model = model;
  this.year = year;
  this.pickupConfig = pickupConfig;
}

// Making a new object    
var telecaster = new guitar('Fender','Telecaster','1952','SS');

// Putting the created object in storage
localStorage.setItem('Fender Tele', JSON.stringify(telecaster));

With setItem I pass in a key and a value. Now my new object is stored in localStorage. I can take a peek in the browser console by typing localStorage. Inside I see

Fender Tele: "{"make":"Fender","model":"Telecaster","year":"1952","pickupConfig":"SS"}"

That’s fine and dandy, but what if I want to pull it out of storage and interact with it like an object again? This is where jQuery’s parseJSON function comes in:

var storedGuitar = jQuery.parseJSON(localStorage.getItem('Fender Tele'));

Then I can use the storedGuitar variable to access its properties like normal:

storedGuitar.model
// => 'Telecaster'

Now say I wanted to store multiple Guitar objects and be able to iterate over them on page load and create markup with their properties. Since I couldn’t think of a good way to “search” through localStorage keys, I created a guitarCount key/value that incremented each time a new Guitar object was created, then updated guitarCount and stored the object with a unique key.

function createNewGuitar(mk,md,y,pc) {
  var createdGuitar = new guitar(mk,md,y,pc);
  if ( localStorage.guitarCount == undefined ) {
	  localStorage.setItem('guitarCount', 0) 
  }
  var guitarSize = parseInt(localStorage.guitarCount) + 1;
  commitToStorage(guitarSize,createdGuitar);
}

function commitToStorage(objectCount,newObject) {
  // The unique key of the object:
  var item = 'guitar_' + objectCount;
  localStorage.setItem('guitarCount', objectCount);
  
  // Put the object into storage
  localStorage.setItem(item, JSON.stringify(newObject));
}

Now I can create two guitars with unique keys and store them in localStorage:

createNewGuitar("Gibson","Les Paul","1963","HH"); 
// guitar_1
createNewGuitar("Gretsch","G6128T-DSV Duo Jet","1962","HH"); 
// guitar_2

If I wanted to do something with these objects on page load, I can loop through each unique key id:

var guitarCount = localStorage.getItem('guitarCount');  
for (i=1;i<=guitarCount;i++)
  {
    var number = parseInt(i) + 1;
	  var guitar = jQuery.parseJSON(localStorage.getItem("guitar_" + i));
    createMarkup(guitar);
  } 

function createMarkup(guitar) {
  $('ul#guitars').append(
    "<li>" + 
      guitar.year + " " +
      guitar.make + " " +
      guitar.model + " " +
      "(" + guitar.pickupConfig + ")" +
    "</li>"
  );
}

I pulled createMarkup() into its own function so it can be easily called on form submission. That way the page doesn’t need to be reloaded to show new data.

I’ve put this code project on Github. I’m not sure how practical it is to use for this type of data storage, but it’s a fun experiment and an introduction to HTML5 localStorage’s possibilities.

I’ve redesigned my website several times and have spent the past few years running Chyrp, a lightweight PHP blogging/tumblogging engine. After I learned the lead developer would no longer actively develop Chyrp1, I stopped watching the community and kind of let my site rot. I was busy doing Relatively Early work where I was exposed to Harmony and I instantly fell in love with it. Building and maintaining client sites on it is a breeze, and it’s fun to do. After learning what I could do with it I decided to port my site over, wanting to simplify things anyway.

Although I will miss being involved in the development community of an open-source blogging engine, moving to Harmony provides me with lots of data flexibility and options without the hassle of maintaining my own software installation. The guys at Ordered List keep rolling out awesome new features. No, it’s not free, but the monthly cost of hosting + a killer CMS is very fair.

As for the site structure, I wanted a simple blog where I could highlight some projects I’ve worked on without feeling like I was juggling a bunch of pages or splitting things up into categories. The home page is just a blog where you can access tags and archives at the bottom or from article headers.

To highlight projects I thought were significant I threw together the campily-named “Lifetime Achievements” section. Clicking on the tagline in the page header reveals an icon grid of blog posts about big projects. I had a little too much fun with CSS3 animations and transitions. The name/date sorting functionality is made possible with Quicksand. I think it will be a fun and fairly painless way to document things I’ve worked on.

The overall design and colors are based on my favorite shirt:

The shirt of Inspiration!

I’ve moved most of my old posts over, but this is more of a fresh start. A webby do-over!


1 Although it seems that semi-recently Chyrp was kicked into active development again and released a 2.1.

About

Ad9aa05f652ec70c232ba38866e57df4?s=60

I like making things on the internet with CremaLab and music with Fullbloods, The Empty Spaces, and Golden Sound Records. I live in Kansas City and enjoy food and drink.