From 65cfb6b69bc7a991604a7cc402eb066ec94e2110 Mon Sep 17 00:00:00 2001 From: Ansis Brammanis Date: Thu, 28 Feb 2013 18:38:35 -0500 Subject: [PATCH] Add asyncMap --- index.html | 15 ++++++--------- js/id/util.js | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/index.html b/index.html index b25a34bca..123cd5778 100644 --- a/index.html +++ b/index.html @@ -186,16 +186,13 @@ var id = iD(); - d3.json('keys.json', function(err, keys) { - d3.json('presets/presets.json', function(err, presets_data) { + iD.util.asyncMap(['keys.json', 'presets/presets.json'], d3.json, function(err, data) { + id.connection() + .keys(data[0]) + .presetData(iD.presetData().data(data[1])); - id.connection() - .keys(keys) - .presetData(iD.presetData().data(presets_data)); - - d3.select("#iD") - .call(id.ui()) - }); + d3.select("#iD") + .call(id.ui()) }); diff --git a/js/id/util.js b/js/id/util.js index 544b1907e..1ef5f87e2 100644 --- a/js/id/util.js +++ b/js/id/util.js @@ -112,3 +112,18 @@ iD.util.fastMouse = function(container) { }; iD.util.getPrototypeOf = Object.getPrototypeOf || function(obj) { return obj.__proto__; }; + +iD.util.asyncMap = function(inputs, func, callback) { + var remaining = inputs.length, + results = [], + errors = []; + + inputs.forEach(function(d, i) { + func(d, function done(err, data) { + errors[i] = err; + results[i] = data; + remaining --; + if (!remaining) callback(errors, results); + }); + }); +};