Last active
August 29, 2015 14:24
-
-
Save Bondifrench/34979c3fcb77649d7984 to your computer and use it in GitHub Desktop.
Retrieving an array in Mithril
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
var Ids = [139502, 92769, 57443]; | |
forecasts.model2 = function(Ids) { | |
console.log(Ids); | |
return m.request({ | |
method: 'GET', | |
url: window.location.origin + '/api/', | |
data: { | |
reportIds: Ids | |
} | |
}) | |
} | |
// this will send a request like this: GET /finviews/api/v0.1/allnormalised/?reportIds=139502&reportIds=92769&reportIds=57443 | |
// and then will be able on the server side like this: | |
server.get(preUrl +'/allnormalised/', function(req, res) { | |
// Need validation of req.query.reportIds | |
console.log(req.query.reportIds) | |
db.findAll({ | |
where: { | |
ReportId: { | |
$in: req.query.reportIds | |
} | |
} | |
}) | |
.then(function(content) { | |
res.json(content); | |
}) | |
.catch(function(error) { | |
console.log('Error retrieving normalised data: ', error); | |
res.status(404).json(error) | |
}); | |
}); | |
// The drawback is everything is aggregated in one single array, so need to do data and recreate a tree structure | |
// The other idea is mapping the array and doing as many requests as elements in the array, using m.sync which is like promise all | |
// | |
var container = []; | |
m.sync(Ids.map(function(i) { | |
return m.request({ | |
method: 'GET', | |
url: window.location.origin + '/api/' + i | |
}) | |
})).then(container) | |
// container is then an array of arrays, so still need to do some work on the data afterwards but at least separated already by Ids | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment