Created
September 14, 2012 04:49
-
-
Save adelevie/3719861 to your computer and use it in GitHub Desktop.
This file contains 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
// implementation of random queries using Parse Cloud Code | |
// see https://parse.com/questions/random-search-its-possibile for setup details | |
Parse.Cloud.define('randomNouns', function(request, response) { | |
var NounMaster = Parse.Object.extend("NounMaster"); | |
var maxQuery = new Parse.Query(NounMaster); | |
maxQuery.first({ | |
success: function(object) { | |
var max = object.get("nextWordIndex"); | |
var n = 10; | |
if(request.params.count) { n = request.params.count; } | |
var arr = []; | |
while (arr.length < n) { | |
arr.push(Math.ceil(Math.random() * max)); | |
} | |
var indexes = arr; | |
var Noun = Parse.Object.extend("Noun"); | |
var nounQuery = new Parse.Query(Noun); | |
nounQuery.containedIn("index", indexes); | |
nounQuery.find({ | |
success: function(results) { | |
response.success(results); | |
} | |
}); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment