Here is a link to a bin with all the required details: http://jsbin.com/ribor/1/edit?js,output
To make answering on SO easier, I will provide the model examples here:
Orders = {
_id: "T487wBz7Pvzjo2QHh",
beverages: [
{
_id: "8ea26bb103efae385f80f881",
delivered: false,
name: "Wine",
units: "55"
}
],
location: "yTKWLFwSDvp3DyCkx",
locationName: "Ladies Garden",
locationNumber: "101",
timestamp: 1398393004864,
user_id: "W3Fdq36Ts2TdWxCRP",
username: "jgeezy"
};
Locations = {
_id: "yTKWLFwSDvp3DyCkx",
beverages: [
{
_id: "e5552a68266ed76895b8228a",
fillTo: "10",
name: "Wine Coolers",
orderWhen: "10",
startUnits: "10"
}
],
name: "Teen Cabana",
number: "103",
organization: "Super Happy Teens!",
vendor: false
};
I am trying to create rows in a table for each location displaying the total number of undelivered beverage orders per location. Though I have a function written for this purpose:
Template.dashboardOrders.undeliveredOrders = ->
undeliveredOrders = Orders.find({'beverages.delivered': false}, {
transform: (order) ->
order.unfilledOrders = 0
_.each order.beverages, (bev) ->
if not bev.delivered
order.unfilledOrders += 1
order
})
The current output is an array sorted by Order and timestamp, whereas I need it sorted by unique location and timestamp. I've tried different map and reduce methods but haven't been successful. Any guidance on how to achieve this would be greatly appreciated!
BONUS Additionally, I feel my approach could be more elegant without having to return the order object and use += to increment. Perhaps there is a better way to handle this.