I have an asynchronous ajax function, complete with a callback:
var coreChat = {
fetch: function (fn) {
$.ajax({
url: "https://api.parse.com/1/classes/chats",
data: {
order: 'createdAt',
limit: 10
},
type: "GET"
})
.done(function(data) {
var messages = [];
for (var i = 0, len = data.results.length; i < len; i++) {
messages.push(data.results[i].text);
}
return fn(messages);
});
},
I'm pretty sure the .done(function(data) {...
portion of the above code is the callback, though feel free to correct me if I'm wrong.
Now I'm getting confused when this fetch
asynchronous function is called later on in the code...
var myChat = {
updateMessages: function () {
this.fetch(function (messages) {
myChat.display(messages);
});
}
};
$.extend(myChat, coreChat);
myChat.init();
In the updateMessages function, fetch
is called with a parameter, which is another function - my question is - is that function the new callback? Does it replace the callback that was originally defined in the fetch
function? Or maybe it's the argument to the callback that's originally called in the fetch function? I'm a bit lost here and any help would be appreciated...
If you'd like a link to the entire code: