I have a function matcher which is called every time a keyup event is sent.
This function belongs to a module which look like this (1).
what if another call is done before the fetching is completed?
How can I solve this problem in this module (1)?
(1)
(function ($, UserCollection) {
var userCollection;
var matcher = function (request, response) {
if (!userCollection) {
userCollection = new UserCollection();
userCollection.fetch();
} else {
isMatched(request, response);
}
};
return matcher;
}(jquery, UserCollection));
I’ll take a different, probably overkill, approach and use the jqXHR object returned by
collection.fetch.If the xhr is already resolved, the callback is immediately invoked, if not, it will be when the request completes : see jQuery.Deferred for more info.
And you would use it as
And a Fiddle http://jsfiddle.net/EWSAV/1/