Last active
November 8, 2018 08:44
-
-
Save hayatoShingu/9073910 to your computer and use it in GitHub Desktop.
It wraps Lawnchair as a provider, allowing you to set global configuration before your dependencies are getting injected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//inspired by https://github.com/wspringer/angular-pouchdb | |
//lawnchar simple wrapper | |
// | |
//It wraps Lawnchair as a provider, allowing you to set global configuration before your | |
//dependencies are getting injected | |
// | |
//It uses $q-based promises instead of callbacks: db.get({...}) will return a promise with the results, | |
//and no longer accepts a callback as the last parameter. | |
//The same goes for all other operations that normally required callbacks. | |
// | |
//It will make sure Angular is aware of asynchronous updates. | |
//(It will make sure it uses $rootScope.$apply() in cases where it makes sense.) | |
// | |
// | |
//USAGE: | |
//new db => $scope.db = $lawnchair.create('name'); | |
//save new value: | |
// $scope.db.save(obj).then( | |
// function(res){console.info(obj + " saved!")}, | |
// function(err){console.error(err);} | |
// ); | |
//and so on. | |
// | |
(function () { | |
var lawnchair, slice; | |
lawnchair = angular.module('local-db-services', []); | |
slice = Array.prototype.slice; | |
lawnchair.provider('$lawnchair', function () { | |
return { | |
$get: function ($q, $rootScope) { | |
var dbMaxSize = 4 * 1024 * 1024; | |
var promiser = function (fn) { | |
return function () { | |
var response_callback , error_callback , deferred, args; | |
deferred = $q.defer(); | |
response_callback = function (res) { | |
deferred.resolve(res); | |
}; | |
error_callback = function (err) { | |
deferred.reject(err); | |
}; | |
args = arguments != null ? slice.call(arguments) : []; | |
args.push(response_callback); | |
args.push(error_callback); | |
fn.apply(this, args); | |
return deferred.promise; | |
}; | |
}; | |
return { | |
create: function (name) { | |
var db; | |
db = new Lawnchair({ | |
name : name, | |
table : name, | |
max : dbMaxSize//, | |
//adapter: /*adapter name*/ | |
}, function () {/*callback for creation*/} | |
); | |
return { | |
save : promiser(db.save.bind(db)), | |
remove: promiser(db.remove.bind(db)), | |
keys : promiser(db.keys.bind(db)), | |
exists: promiser(db.exists.bind(db)), | |
batch : promiser(db.batch.bind(db)), | |
all : promiser(db.all.bind(db)), | |
get : promiser(db.get.bind(db)), | |
nuke : promiser(db.nuke.bind(db)), | |
valid : promiser(db.valid.bind(db)) | |
}; | |
} | |
}; | |
} | |
}; | |
}); | |
}).call(this); |
Is used to invoke the function. And "this" is the context. In this case the context is not strictly necessary.
As I have defined it: "(function(){}).call(this)" without the "call" function is not executed.
added function isArray
updated.
right context of the function. (with .bind)
remover wrongly exposed methods.
Now it works with other lawnchair adapter (indexed-db, webkit-sql...)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why the .call(this) ?