I am making an asynchronous 'GET' request using the Parse API. The AJAX request returns an array of strings that may not be in the right order. I have searched on Google for an 'order' property AJAX may possess but no luck.
This is my AJAX request as a function called retrieve getting the value of the text property.
AJAX Request:
Obj.retrieve = function(callBack) {
$.ajax({
url : '',
type : 'GET',
dataType: 'JSON',
contentType : 'application/json',
data : JSON.stringify({
text : 'value: ',
order: "-createdAt" // Would the order property go here?
}),
error : function(data) {
console.log('error: ' + data);
},
success : function(data) {
for (var i = 0; i < data["results"].length; i++)
callBack(data["results"][i].text);
}
});
};
TL;DR: Each object has field names createdAt, updatedAt, and objectId thanks to Parse. Ideally, I would like to use createdAt with the order. Any suggestions?
I am making an asynchronous 'GET' request using the Parse API. The AJAX request returns an array of strings that may not be in the right order. I have searched on Google for an 'order' property AJAX may possess but no luck.
This is my AJAX request as a function called retrieve getting the value of the text property.
AJAX Request:
Obj.retrieve = function(callBack) {
$.ajax({
url : 'https://api.parse./1/classes/messages',
type : 'GET',
dataType: 'JSON',
contentType : 'application/json',
data : JSON.stringify({
text : 'value: ',
order: "-createdAt" // Would the order property go here?
}),
error : function(data) {
console.log('error: ' + data);
},
success : function(data) {
for (var i = 0; i < data["results"].length; i++)
callBack(data["results"][i].text);
}
});
};
TL;DR: Each object has field names createdAt, updatedAt, and objectId thanks to Parse. Ideally, I would like to use createdAt with the order. Any suggestions?
Possible solutions:
Sort it on the server side
Sort it manually:
data['results'].sort(function(a, b) {
return a.createdAt < b.createdAt ? -1 : 1;
});
The data
argument in your $.ajax
request gets sent to the server; so you would use that on the server to sort your results.
If you're not sorting on the server, you need to sort in your success:
callback. Suggest you use something like this:
Obj.retrieve = function(sortBy, callBack) {
$.ajax({
url : 'https://api.parse./1/classes/messages',
type : 'GET',
dataType: 'JSON',
contentType : 'application/json',
data : JSON.stringify({
text : 'value: ' // maybe not needed?
}),
error : function(data) {
console.log('error: ' + data);
},
success: function (data) {
data['results'].sort(function (a, b) {
return a.sortBy - b.sortBy;
});
$.each(data['results'], function (n, v) {
callBack(v.text);
});
});
};
Warning: This was piled in my head.
Your usage would be something like o.retrieve('createdAt', function () { });
, or o.retrieve('updatedAt', function () { });
, or o.retrieve('objectId', function () { });
You can just use the order method as your data type and use -.
Obj.retrieve = function(callBack) {
$.ajax({
url : 'https://api.parse./1/classes/messages',
type : 'GET',
dataType: 'JSON',
contentType : 'application/json',
data : 'order=-createdAt', //REQUEST FROM NEWEST
}),
error : function(data) {
console.log('error: ' + data);
},
success : function(data) {
for (var i = 0; i < data["results"].length; i++)
callBack(data["results"][i].text);
}
});
};
From http://api.jquery./jquery.getjson/ we read:
So for this, we should be able to rework the request URL to be: https://api.parse./1/classes/chats?order=createdAt