Skip to content

Commit e894a0e

Browse files
authored
Merge pull request #249 from fohletex/master
Add customdriver method to Persist module
2 parents 5708445 + 8cd7b0e commit e894a0e

File tree

5 files changed

+1351
-1209
lines changed

5 files changed

+1351
-1209
lines changed

doc/Persist.js.html

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,12 @@ <h1 class="page-title">Source: Persist.js</h1>
7676

7777
// Check environment
7878
if (db.isClient()) {
79-
if (window.Storage !== undefined) {
80-
this.mode('localforage');
79+
this.mode('localforage');
8180

82-
localforage.config({
83-
name: String(db.core().name()),
84-
storeName: 'FDB'
85-
});
86-
}
81+
localforage.config({
82+
name: String(db.core().name()),
83+
storeName: 'FDB'
84+
});
8785
}
8886
};
8987

@@ -190,6 +188,31 @@ <h1 class="page-title">Source: Persist.js</h1>
190188
return this._mode;
191189
};
192190

191+
/**
192+
* Sets - if Persist is load in "localforage" mode - a custom driver which applies to the rules
193+
* given by localforage, given here: https://localforage.github.io/localForage/#driver-api-definedriver
194+
* @param driver A local forage driver definition
195+
* @param callback A possible callback, which is executed after the customdriver was initialized
196+
*/
197+
Persist.prototype.customdriver = function(driver, callback) {
198+
if (this.mode() !== 'localforage') {
199+
throw 'No interface ready to set custom driver!';
200+
}
201+
202+
var driverName = driver._driver; // copying the drivername to avoid possible variable modification errors
203+
localforage.defineDriver(driver).then(function() {
204+
return localforage.setDriver(driverName);
205+
})
206+
.then(function() {
207+
if (callback) {
208+
callback(null);
209+
}
210+
})
211+
.catch(function(err) {
212+
callback(err);
213+
});
214+
};
215+
193216
/**
194217
* Gets / sets the driver used when persisting data.
195218
* @param {String} val Specify the driver type (LOCALSTORAGE,

js/lib/Persist.js

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,12 @@ Persist.prototype.init = function (db) {
4848

4949
// Check environment
5050
if (db.isClient()) {
51-
if (window.Storage !== undefined) {
52-
this.mode('localforage');
51+
this.mode('localforage');
5352

54-
localforage.config({
55-
name: String(db.core().name()),
56-
storeName: 'FDB'
57-
});
58-
}
53+
localforage.config({
54+
name: String(db.core().name()),
55+
storeName: 'FDB'
56+
});
5957
}
6058
};
6159

@@ -162,6 +160,31 @@ Persist.prototype.mode = function (type) {
162160
return this._mode;
163161
};
164162

163+
/**
164+
* Sets - if Persist is load in "localforage" mode - a custom driver which applies to the rules
165+
* given by localforage, given here: https://localforage.github.io/localForage/#driver-api-definedriver
166+
* @param driver A local forage driver definition
167+
* @param callback A possible callback, which is executed after the customdriver was initialized
168+
*/
169+
Persist.prototype.customdriver = function(driver, callback) {
170+
if (this.mode() !== 'localforage') {
171+
throw 'No interface ready to set custom driver!';
172+
}
173+
174+
var driverName = driver._driver; // copying the drivername to avoid possible variable modification errors
175+
localforage.defineDriver(driver).then(function() {
176+
return localforage.setDriver(driverName);
177+
})
178+
.then(function() {
179+
if (callback) {
180+
callback(null);
181+
}
182+
})
183+
.catch(function(err) {
184+
callback(err);
185+
});
186+
};
187+
165188
/**
166189
* Gets / sets the driver used when persisting data.
167190
* @param {String} val Specify the driver type (LOCALSTORAGE,

0 commit comments

Comments
 (0)