From f808b047cd5c3105a4c32b9e4fccd3616384bd11 Mon Sep 17 00:00:00 2001 From: Jake Harding Date: Wed, 13 Mar 2013 21:22:08 -0700 Subject: [PATCH] Don't cache unnamed datasets. Fixes #111. --- src/dataset.js | 39 ++++++++++++++++++++++++++------------- src/typeahead.js | 12 +++++++----- test/dataset_spec.js | 36 ++++++++++++++++++++++++++++++------ 3 files changed, 63 insertions(+), 24 deletions(-) diff --git a/src/dataset.js b/src/dataset.js index eda0b9c0..c46884ca 100644 --- a/src/dataset.js +++ b/src/dataset.js @@ -17,13 +17,13 @@ var Dataset = (function() { $.error('one of local, prefetch, or remote is requried'); } - this.name = o.name; + this.name = o.name || utils.getUniqueId(); this.limit = o.limit || 5; this.header = o.header; this.footer = o.footer; this.template = compileTemplate(o.template, o.engine); - // used in #initialize + // used then deleted in #initialize this.local = o.local; this.prefetch = o.prefetch; this.remote = o.remote; @@ -37,7 +37,10 @@ var Dataset = (function() { this.itemHash = {}; this.adjacencyList = {}; - this.storage = new PersistentStorage(o.name); + + // only initialize storage if there's a name otherwise + // loading from storage on subsequent page loads is impossible + this.storage = o.name ? new PersistentStorage(o.name) : null; } utils.mixin(Dataset.prototype, { @@ -52,11 +55,19 @@ var Dataset = (function() { _loadPrefetchData: function(o) { var that = this, deferred, - version = this.storage.get(this.keys.version), - protocol = this.storage.get(this.keys.protocol), - itemHash = this.storage.get(this.keys.itemHash), - adjacencyList = this.storage.get(this.keys.adjacencyList), - isExpired = version !== VERSION || protocol !== utils.getProtocol(); + version, + protocol, + itemHash, + adjacencyList, + isExpired; + + if (this.storage) { + version = this.storage.get(this.keys.version); + protocol = this.storage.get(this.keys.protocol); + itemHash = this.storage.get(this.keys.itemHash); + adjacencyList = this.storage.get(this.keys.adjacencyList); + isExpired = version !== VERSION || protocol !== utils.getProtocol(); + } o = utils.isString(o) ? { url: o } : o; o.ttl = utils.isNumber(o.ttl) ? o.ttl : 24 * 60 * 60 * 1000; @@ -83,12 +94,14 @@ var Dataset = (function() { itemHash = processedData.itemHash, adjacencyList = processedData.adjacencyList; - // store process data in local storage + // store process data in local storage, if storage is available // this saves us from processing the data on every page load - that.storage.set(that.keys.itemHash, itemHash, o.ttl); - that.storage.set(that.keys.adjacencyList, adjacencyList, o.ttl); - that.storage.set(that.keys.version, VERSION, o.ttl); - that.storage.set(that.keys.protocol, utils.getProtocol(), o.ttl); + if (that.storage) { + that.storage.set(that.keys.itemHash, itemHash, o.ttl); + that.storage.set(that.keys.adjacencyList, adjacencyList, o.ttl); + that.storage.set(that.keys.version, VERSION, o.ttl); + that.storage.set(that.keys.protocol, utils.getProtocol(), o.ttl); + } that._mergeProcessedData(processedData); } diff --git a/src/typeahead.js b/src/typeahead.js index 062f4a9e..42a5d617 100644 --- a/src/typeahead.js +++ b/src/typeahead.js @@ -5,7 +5,7 @@ */ (function() { - var datasetCache = {}, viewKey = 'ttView', methods; + var cache = {}, viewKey = 'ttView', methods; methods = { initialize: function(datasetDefs) { @@ -22,11 +22,13 @@ } datasets = utils.map(datasetDefs, function(o) { - o.name = o.name || utils.getUniqueId(); + var dataset = cache[o.name] ? cache[o.name] : new Dataset(o); - return datasetCache[o.name] ? - datasetCache[o.name] : - datasetCache[o.name] = new Dataset(o); + if (o.name) { + cache[o.name] = dataset; + } + + return dataset; }); return this.each(initialize); diff --git a/test/dataset_spec.js b/test/dataset_spec.js index 02a6a405..a75b5f1b 100644 --- a/test/dataset_spec.js +++ b/test/dataset_spec.js @@ -55,9 +55,26 @@ describe('Dataset', function() { // -------------- describe('#constructor', function() { - it('should initialize persistent storage', function() { - expect(new Dataset({ local: fixtureData }).storage).toBeDefined(); - expect(PersistentStorage).toHaveBeenCalled(); + describe('when called with a name', function() { + beforeEach(function() { + this.dataset = new Dataset({ name: '#constructor', local: fixtureData }); + }); + + it('should initialize persistent storage', function() { + expect(this.dataset.storage).toBeDefined(); + expect(PersistentStorage).toHaveBeenCalled(); + }); + }); + + describe('when called with no name', function() { + beforeEach(function() { + this.dataset = new Dataset({ local: fixtureData }); + }); + + it('should not use persistent storage', function() { + expect(this.dataset.storage).toBeNull(); + expect(PersistentStorage).not.toHaveBeenCalled(); + }); }); describe('when called with a template but no engine', function() { @@ -137,7 +154,11 @@ describe('Dataset', function() { describe('when called with prefetch', function() { describe('if data is available in storage', function() { beforeEach(function() { - this.dataset = new Dataset({ prefetch: '/prefetch.json' }); + this.dataset = new Dataset({ + name: 'prefetch', + prefetch: '/prefetch.json' + }); + this.dataset.storage.get.andCallFake(mockStorageFns.getHit); this.dataset.initialize(); }); @@ -164,6 +185,7 @@ describe('Dataset', function() { beforeEach(function() { this.dataset = new Dataset({ + name: 'prefetch', prefetch: { url: '/prefetch.json', filter: function(data) { return ['filter']; } @@ -203,10 +225,12 @@ describe('Dataset', function() { describe('if filter was not passed in', function() { beforeEach(function() { - this.dataset = new Dataset({ prefetch: '/prefetch.json' }); + this.dataset = new Dataset({ + name: 'prefetch', + prefetch: '/prefetch.json' + }); this.dataset.storage.get.andCallFake(mockStorageFns.getMiss); - this.dataset.initialize(); this.request = mostRecentAjaxRequest();