|
83 | 83 | return deferred; |
84 | 84 |
|
85 | 85 | function handlePrefetchResponse(resp) { |
86 | | - var filtered; |
87 | | - |
88 | | - filtered = o.filter ? o.filter(resp) : resp; |
89 | | - that.add(filtered); |
| 86 | + // reset to mirror the behavior of bootstrapping |
| 87 | + that.reset(); |
| 88 | + that.add(o.filter ? o.filter(resp) : resp); |
90 | 89 |
|
91 | 90 | that._saveToStorage(that.index.serialize(), o.thumbprint, o.ttl); |
92 | 91 | } |
|
105 | 104 | return this.transport.get(url, this.remote.ajax, handleRemoteResponse); |
106 | 105 |
|
107 | 106 | function handleRemoteResponse(resp) { |
108 | | - var filtered = that.remote.filter ? that.remote.filter(resp) : resp; |
109 | | - |
110 | | - cb(filtered); |
| 107 | + cb(that.remote.filter ? that.remote.filter(resp) : resp); |
111 | 108 | } |
112 | 109 | }, |
113 | 110 |
|
|
136 | 133 | return stored.data && !isExpired ? stored.data : null; |
137 | 134 | }, |
138 | 135 |
|
139 | | - // ### public |
140 | | - |
141 | | - // the contents of this function are broken out of the constructor |
142 | | - // to help improve the testability of bloodhounds |
143 | | - initialize: function initialize() { |
144 | | - var that = this, deferred; |
| 136 | + _initialize: function initialize() { |
| 137 | + var that = this, local = this.local, deferred; |
145 | 138 |
|
146 | 139 | deferred = this.prefetch ? |
147 | 140 | this._loadPrefetch(this.prefetch) : $.Deferred().resolve(); |
148 | 141 |
|
149 | | - // local can be a function that returns an array of datums |
150 | | - this.local = _.isFunction(this.local) ? this.local() : this.local; |
151 | | - |
152 | 142 | // make sure local is added to the index after prefetch |
153 | | - this.local && deferred.done(addLocalToIndex); |
| 143 | + local && deferred.done(addLocalToIndex); |
154 | 144 |
|
155 | 145 | this.transport = this.remote ? new Transport(this.remote) : null; |
156 | | - this.initialize = function initialize() { return deferred.promise(); }; |
157 | 146 |
|
158 | | - return deferred.promise(); |
| 147 | + return (this.initPromise = deferred.promise()); |
| 148 | + |
| 149 | + function addLocalToIndex() { |
| 150 | + // local can be a function that returns an array of datums |
| 151 | + that.add(_.isFunction(local) ? local() : local); |
| 152 | + } |
| 153 | + }, |
| 154 | + |
| 155 | + // ### public |
159 | 156 |
|
160 | | - function addLocalToIndex() { that.add(that.local); } |
| 157 | + initialize: function initialize(force) { |
| 158 | + !this.initPromise || force ? this._initialize() : this.initPromise; |
161 | 159 | }, |
162 | 160 |
|
163 | 161 | add: function add(data) { |
|
201 | 199 | } |
202 | 200 | }, |
203 | 201 |
|
| 202 | + reset: function reset() { |
| 203 | + this.index.reset(); |
| 204 | + }, |
| 205 | + |
| 206 | + clearPrefetchCache: function clearPrefetchCache() { |
| 207 | + this.storage && this.storage.clear(); |
| 208 | + }, |
| 209 | + |
204 | 210 | ttAdapter: function ttAdapter() { return _.bind(this.get, this); } |
205 | 211 | }); |
206 | 212 |
|
|
0 commit comments