Whenever I do :
$.ajax({
//my settings
}).done(function(data){
// Publish or store data
});
It works perfectly fine! however, I’m trying to apply the observer pattern, because I’m required to, but when I do, using this code:
$.ajax({
// my settings
}).done(function(data){
$(document).trigger('ALLREADY',data);
});
// Some other part of the code
$(document).on('ALLREADY',function(e,data){
console.log(data); // WHY?!
});
The response (data) is only storing the first value, not the whole array, as it does with the first example, what did i do to break the functionality?
PS: Note that even though it’s returning only the first value of a supposedly big array, even when i try to use it, it won’t let me, however, i can log it.
Since
datais an array, each individual piece is sent as a separate argument in the event callback. It’s strange because ifdatawere an object this would not be the case (you’d be able to use it likedata).Instead, just use
data = arguments.slice(1)