Skip to content

Commit 7e22620

Browse files
author
Jake Harding
committedApr 26, 2015
Add prepare to prefetch
1 parent 4ac1777 commit 7e22620

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed
 

‎doc/bloodhound.md

+7
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,13 @@ When configuring `prefetch`, the following options are available.
225225
* `thumbprint` – A string used for thumbprinting prefetched data. If this
226226
doesn't match what's stored in local storage, the data will be refetched.
227227

228+
* `prepare` – A function that provides a hook to allow you to prepare the
229+
settings object passed to `transport` when a request is about to be made.
230+
The function signature should be `prepare(settings)` where `settings` is the
231+
default settings object created internally by the Bloodhound instance. The
232+
`prepare` function should return a settings object. Defaults to the
233+
[identity function].
234+
228235
* `transform` – A function with the signature `transform(response)` that allows
229236
you to transform the prefetch response before the Bloodhound instance operates
230237
on it. Defaults to the [identity function].

‎src/bloodhound/options_parser.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ var oParser = (function() {
4949
cache: true,
5050
cacheKey: null,
5151
thumbprint: '',
52+
prepare: _.identity,
5253
transform: _.identity,
5354
transport: null
5455
};
@@ -98,7 +99,7 @@ var oParser = (function() {
9899
// DEPRECATED: filter will be dropped in v1
99100
o.transform = o.filter || o.transform;
100101

101-
o.prepare = toPrepare(o);
102+
o.prepare = toRemotePrepare(o);
102103
o.limiter = toLimiter(o);
103104
o.transport = o.transport ? callbackToDeferred(o.transport) : $.ajax;
104105

@@ -110,7 +111,7 @@ var oParser = (function() {
110111
return o;
111112
}
112113

113-
function toPrepare(o) {
114+
function toRemotePrepare(o) {
114115
var prepare, replace, wildcard;
115116

116117
prepare = o.prepare;

‎src/bloodhound/prefetch.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ var Prefetch = (function() {
1919
this.url = o.url;
2020
this.ttl = o.ttl;
2121
this.cache = o.cache;
22+
this.prepare = o.prepare;
2223
this.transform = o.transform;
2324
this.transport = o.transport;
2425
this.thumbprint = o.thumbprint;
@@ -69,11 +70,12 @@ var Prefetch = (function() {
6970
},
7071

7172
fromNetwork: function(cb) {
72-
var that = this;
73+
var that = this, settings;
7374

7475
if (!cb) { return; }
7576

76-
this.transport(this._settings()).fail(onError).done(onResponse);
77+
settings = this.prepare(this._settings());
78+
this.transport(settings).fail(onError).done(onResponse);
7779

7880
function onError() { cb(true); }
7981
function onResponse(resp) { cb(null, that.transform(resp)); }

‎test/bloodhound/prefetch_spec.js

+12
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,18 @@ describe('Prefetch', function() {
108108
});
109109
});
110110

111+
it('should transform request settings with prepare', function() {
112+
var spy;
113+
114+
spy = jasmine.createSpy();
115+
spyOn(this.prefetch, 'prepare').andReturn({ foo: 'bar' });
116+
spyOn(this.prefetch, 'transport').andReturn($.Deferred());
117+
118+
this.prefetch.fromNetwork(spy);
119+
120+
expect(this.prefetch.transport).toHaveBeenCalledWith({ foo: 'bar' });
121+
});
122+
111123
it('should transform the response using transform', function() {
112124
var spy;
113125

0 commit comments

Comments
 (0)
Please sign in to comment.