Skip to content

Instantly share code, notes, and snippets.

@hayatoShingu
Last active November 8, 2018 08:44
Show Gist options
  • Save hayatoShingu/9073910 to your computer and use it in GitHub Desktop.
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
//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);
@nicolasembleton
Copy link

Why the .call(this) ?

@hayatoShingu
Copy link
Author

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.

@hayatoShingu
Copy link
Author

added function isArray

@hayatoShingu
Copy link
Author

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