Created
January 16, 2014 20:04
-
-
Save ryanpitts/8462387 to your computer and use it in GitHub Desktop.
yay underscore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// hit the /parents API endpoint | |
$.getJSON(parentGeoAPI) | |
.done(function(results) { | |
var parents = results['parents']; | |
// list of unique parent sumlev types, maintaining order | |
var parentRelations = _.uniq(_.pluck(parents, 'sumlevel')); | |
// collect parents into individual sumlev groups | |
var parentGroups = _.groupBy(parents, function(d) { | |
return d.sumlevel; | |
}); | |
// for each parent sumlev type ... | |
var parentLinkSets = _.map(parentRelations, function(r) { | |
// ... compile a set of links to individual profile pages | |
var parentLinkSet = _.map(parentGroups[r], function(v, k) { | |
return '<a href="/profiles/' + v.geoid + '/">' + v.display_name + '</a>'; | |
}); | |
var numParents = parentLinkSet.length; | |
// if more than one of a sumlev type, group behind reveal link | |
if (numParents > 1) { | |
return '<a href="#" class="link-reveal">'+numParents+' '+ sumlevMap[r]['plural'] +'</a><span class="hidden">'+parentLinkSet.join(', ')+'</span>'; | |
} else { | |
// just one of this sumlev type, so add it to list | |
return parentLinkSet; | |
} | |
}) | |
// push the whole thing into the header box ... thingy | |
parentLinkContainer.html(parentLinksPrefix + parentLinkSets.join(', ')); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// hit the /parents API endpoint | |
$.getJSON(parentGeoAPI) | |
.done(function(results) { | |
var parents = results['parents'] | |
parentGroups = {}, | |
parentRelations = [], | |
parentLinks = []; | |
// loop through the results so we can group by sumlev type | |
parents.forEach(function(d) { | |
var relation = d.relation; | |
// build ordered list of unique parent sumlev types | |
if (parentRelations.indexOf(relation) == -1) { | |
parentRelations.push(relation) | |
} | |
// collect parents into sumlev groups | |
parentGroups[d.relation] = parentGroups[d.relation] || {}; | |
parentGroups[d.relation][d.geoid] = d; | |
}) | |
// loop through the parent sumlev types | |
parentRelations.forEach(function(r) { | |
var parentKeys = Object.keys(parentGroups[r]), | |
numParents = parentKeys.length, | |
parentLinkSet = []; | |
// create basic profile links for each parent... | |
parentKeys.forEach(function(k) { | |
var d = parentGroups[r][k]; | |
parentLinkSet.push('<a href="/profiles/' + d.geoid + '/">' + d.display_name + '</a>'); | |
}) | |
// ... and group them into comma-separated list | |
parentLinkSet = parentLinkSet.join(', '); | |
// hide sumlev groups with more than one parent, | |
// and provide trigger that unhides them | |
if (numParents > 1) { | |
parentLinks.push('<a href="#" class="link-reveal">'+numParents+' '+ r +'</a><span class="hidden">'+parentLinkSet+'</span>'); | |
} else { | |
parentLinks.push(parentLinkSet); | |
} | |
}) | |
parentLinkContainer.html(parentLinksPrefix + parentLinks.join(', ')); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment