Created
June 6, 2012 14:26
-
-
Save vseventer/2882217 to your computer and use it in GitHub Desktop.
JavaScript Library aggregation leaderboard
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
| // Scores collection | |
| // [{ user: 'Alice', score: 2 }, | |
| // { user: 'Alice', score: 1 }, | |
| // { user: 'Bob', score: 4 }] | |
| // Create leaderboard | |
| var agg = new Kinvey.Aggregation(); | |
| agg.on('user'); | |
| agg.setInitial({ total: 0 }); | |
| agg.setReduce(function(doc, out) { | |
| out.total += doc.score; | |
| }); | |
| var collection = new Kinvey.Collection('scores'); | |
| collection.aggregate(agg, { | |
| success: function(response) { | |
| // response holds: | |
| // [{ name: 'Alice', total: 3 }, | |
| // {name: 'Bob', total: 4 }] | |
| }, | |
| error: function(error) { | |
| // Oops. An error occured. | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment