Skip to content

Instantly share code, notes, and snippets.

@cgkio
Created November 18, 2013 17:39
Show Gist options
  • Save cgkio/7531958 to your computer and use it in GitHub Desktop.
Save cgkio/7531958 to your computer and use it in GitHub Desktop.
Boilerplate for basic Parse.com query (for JQM listview)
var Query = Parse.Object.extend("parse_class");
var query = new Parse.Query(Query);
//set query conditions
query.ascending("parse_item"); //sorts the results in ascending order by the score field
query.descending("score"); //sorts the results in descending order by the score field
query.limit(10); //limit to at most 10 results
query.skip(10); //skip the first 10 results
query.equalTo("playerEmail", "[email protected]");
query.notEqualTo("playerName", "Michael Yabuti");
query.lessThan("wins", 50); //restricts to wins < 50
query.lessThanOrEqualTo("wins", 50); //restricts to wins <= 50
query.greaterThan("wins", 50); //restricts to wins > 50
query.greaterThanOrEqualTo("wins", 50); //restricts to wins >= 50
query.containedIn("playerName", ["Jonathan Walsh", "Dario Wunsch", "Shawn Simon"]); //finds scores from any of Jonathan, Dario, or Shawn
query.notContainedIn("playerName", ["Jonathan Walsh", "Dario Wunsch", "Shawn Simon"]); //finds scores from anyone who is neither Jonathan, Dario, nor Shawn
query.exists("score"); //finds objects that have the score set
query.doesNotExist("score"); //finds objects that don't have the score set
query.equalTo("arrayKey", 2); //find objects where the array in arrayKey contains 2
query.containsAll("arrayKey", [2, 3, 4]); //find objects where the array in arrayKey contains all of the elements 2, 3, and 4
query.startsWith("name", "Big Daddy's"); //finds names that start with "Big Daddy's"
//set query parameters
query.find({ //shows all query results
query.first({ //shows only the first query result
success: function(results) {
var output = "";
$.each(results, function(i,result){
output += '<li><a href="#" onclick="function_name(' + result.id + ')"><h3>' + results[i].get("parse_item") + '</h3></a></li>';
});
$('#listviewid').append(output).listview('refresh');
},
error: function(error) {
console.log("Error: " + error.code + " " + error.message);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment