// Service for checking the availability of a given word Splitter.module('Checkers', function(Checkers, Splitter) { var LocalChecker = { comKeyFor: function(compound) { return 'avail/.com/' + compound; }, check: function(compound) { return $.jStorage.get(this.comKeyFor(compound)); }, store: function(compound, availability) { var key = ''; // Don't want to store error states. if (availability === 'available' || availability === 'unavailable') { key = this.comKeyFor(compound); // Expire the value in 10 mins. return $.jStorage.set(key, availability, { TTL: 600000 }) }; return false; } }; // Class which can check the availability of a word. var Checker = { check: function(compound, options) { options || (options = {}); // console.log("Checking availability of", compound); // No-op if the word is blank. if ($.trim(compound).length === 0) { return; }; var localAvailability = LocalChecker.check(compound); if (localAvailability) { // console.log("Local availability of", compound + ".com", localAvailability); options.success(localAvailability); } else { $.ajax({ url: '/availabilities/' + encodeURIComponent(compound), dataType: 'json', success: function(json) { var availability = json.status; // console.log("Remote availability of", compound + ".com", availability); LocalChecker.store(compound, availability); options.success(availability); }, error: options.error }); } } }; Checkers.check = function(word, success) { Checker.check(word, success); }; // Create a debounced version of the checking method. Checkers.debouncedCheck = _.debounce(Checkers.check, 300); });