Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- app.factory("TodoService",['$log','pouchDB','$window',function($log,pouchDB,$window){
- $window.PouchDB = pouchdb;
- var db = pouchDB('todos');
- var instance = {};
- instance.get = function get(){
- var res = db.allDocs({include_docs: true, descending: true})
- .then(function(data){
- var pluck = function pluck(data,prop)
- {
- var res = [];
- for(var i=0;i<data.length;i++)
- {
- res.push(data[i][prop]);
- }
- return res;
- };
- var _res = pluck(data.rows,'doc');
- return _res;
- }).catch(function(err){
- $log.error("The error is \n\t",err);
- $log.error("The stacktrace is \n\t",err.stack);
- });
- return res;
- };
- instance.getOne = function getOne(id){
- var res = db.get(id)
- .then(function(data){
- return data;
- })
- .catch(function(err){
- console.log(err,err.stack);
- });
- return res;
- };
- instance.edit = function edit(todo)
- {
- var promise = db.put(todo);
- promise.then(function(response){
- $log.info("Successfully updated the todo item\t",response);
- }).catch(function(err){
- $log.error('An error occured when updating todo');
- $log.error(err,"\n",err.stack);
- });
- };
- instance.save = function save(todo){
- var isOk = function isOk(response)
- {
- if(response.ok)
- $log.info("The document has been saved with id ",response);
- return response;
- };
- var uhOh = function uhOh(err)
- {
- $log.error(err);
- $log.error(err.stack);
- };
- var promise = db.post(todo)
- .then(isOk)
- .catch(uhOh);
- return promise;
- };
- instance.deleteItem = function(todo){
- $log.info("Inside delete ","todo value is ",todo);
- db.remove(todo);
- };
- return instance;
- }]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement