Skip to content

Commit e664f04

Browse files
author
Jake Harding
committed
Add clear and reset functionality to Bloodhound.
1 parent a8f5185 commit e664f04

2 files changed

Lines changed: 32 additions & 22 deletions

File tree

‎src/bloodhound/bloodhound.js‎

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,9 @@
8383
return deferred;
8484

8585
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);
9089

9190
that._saveToStorage(that.index.serialize(), o.thumbprint, o.ttl);
9291
}
@@ -105,9 +104,7 @@
105104
return this.transport.get(url, this.remote.ajax, handleRemoteResponse);
106105

107106
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);
111108
}
112109
},
113110

@@ -136,28 +133,29 @@
136133
return stored.data && !isExpired ? stored.data : null;
137134
},
138135

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;
145138

146139
deferred = this.prefetch ?
147140
this._loadPrefetch(this.prefetch) : $.Deferred().resolve();
148141

149-
// local can be a function that returns an array of datums
150-
this.local = _.isFunction(this.local) ? this.local() : this.local;
151-
152142
// make sure local is added to the index after prefetch
153-
this.local && deferred.done(addLocalToIndex);
143+
local && deferred.done(addLocalToIndex);
154144

155145
this.transport = this.remote ? new Transport(this.remote) : null;
156-
this.initialize = function initialize() { return deferred.promise(); };
157146

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
159156

160-
function addLocalToIndex() { that.add(that.local); }
157+
initialize: function initialize(force) {
158+
!this.initPromise || force ? this._initialize() : this.initPromise;
161159
},
162160

163161
add: function add(data) {
@@ -201,6 +199,14 @@
201199
}
202200
},
203201

202+
reset: function reset() {
203+
this.index.reset();
204+
},
205+
206+
clearPrefetchCache: function clearPrefetchCache() {
207+
this.storage && this.storage.clear();
208+
},
209+
204210
ttAdapter: function ttAdapter() { return _.bind(this.get, this); }
205211
});
206212

‎src/bloodhound/search_index.js‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ var SearchIndex = (function() {
1919
this.datumTokenizer = o.datumTokenizer;
2020
this.queryTokenizer = o.queryTokenizer;
2121

22-
this.datums = [];
23-
this.trie = newNode();
22+
this.reset();
2423
}
2524

2625
// instance methods
@@ -96,6 +95,11 @@ var SearchIndex = (function() {
9695
_.map(unique(matches), function(id) { return that.datums[id]; }) : [];
9796
},
9897

98+
reset: function reset() {
99+
this.datums = [];
100+
this.trie = newNode();
101+
},
102+
99103
serialize: function serialize() {
100104
return { datums: this.datums, trie: this.trie };
101105
}

0 commit comments

Comments
 (0)