Skip to content

Instantly share code, notes, and snippets.

@Olical
Created March 31, 2011 16:27
Show Gist options
  • Save Olical/896697 to your computer and use it in GitHub Desktop.
Save Olical/896697 to your computer and use it in GitHub Desktop.
Blog post: Data normalisation with Mappa
var newsFeed = {
meta: {
foo: 'bar',
etc: 12345
},
results: [
{
t: 'New GCC compiler',
more: {
u: 'http://reddit.com/'
}
},
{
t: 'Some other news',
more: {
u: 'http://bbc.co.uk/'
}
}
]
};
var webDevelopers = {
title: 'Oliver Caldwell',
url: 'http://flowdev.co.uk/'
}
// First we add an alias so we can access the object under the name we want
// Lets call it tb, short for toolbox.
Mappa.addAlias('tb');
// From now on we will reference the Mappa object by its alias, tb.
// Lets map to our animation function
tb.addMap('animate', someAnimateFunction);
// Now lets map to our JSON functions
tb.addMap('json', {
encode: JSON.stringify,
decode: JSON.parse
});
Mappa.addMap('newsFeed', {
title: 'results.t',
url: 'results.more.u'
});
Mappa.addMap('webDevelopers', {
title: 'title',
url: 'url'
});
New GCC compiler (http://reddit.com/)
Some other news (http://bbc.co.uk/)
Oliver Caldwell (http://flowdev.co.uk/)
var norm = Mappa.normalise({
newsFeed: newsFeed,
webDevelopers: webDevelopers
});
for(var i = 0; i < norm.length; i++) {
console.log(norm[i].title + ' (' + norm[i].url + ')');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment