daily pastebin goal
27%
SHARE
TWEET

Untitled

a guest Sep 19th, 2016 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. factory('todoJsonStorage',['$q', function ($q) {
  2. 'use strict';
  3.  
  4. var COLLECTION_NAME = 'Users';
  5. var collections = {
  6.         Users: {
  7.             searchFields: {UserId: 'string', password: 'string'}
  8.         },
  9.     };
  10. var options = {};
  11.  
  12. //Optional username
  13. options.username = 'testuser';
  14. //Optional password
  15. options.password = 'test123';
  16.  
  17.  //Optional local key generation flag
  18.  options.localKeyGen = true;
  19.  
  20. var inited = false;
  21.  
  22. //checks if inited and if not inits
  23. function initJSONStore(){
  24.     var initDeferred = $q.defer();
  25.     if (inited){
  26.         initDeferred.resolve();
  27.     } else {
  28.         //Initialize the collection
  29.         WL.JSONStore.init(collections,options).then(function () {
  30.             console.log("-> JSONStore init successful");
  31.             initDeferred.resolve();
  32.         }).fail(function (errorObject) {
  33.             console.log("-> JSONStore error: " + errorObject.msg);
  34.         });
  35.  
  36.         return initDeferred.promise;
  37.     };
  38. }
  39.  
  40. return {
  41.     get: function () {
  42.         var deferred = $q.defer();
  43.         initJSONStore().then(function(){
  44.             WL.JSONStore.get(COLLECTION_NAME).findAll().then(function (res) {
  45.                 if (res.length > 0){
  46.                     deferred.resolve(JSON.parse(res[0].json.data || '[]'));
  47.                 } else {
  48.                       deferred.resolve(res);                  
  49.                       }
  50.  
  51.             }).fail(function (errorObject) {
  52.                 console.log("JSONStore findbyid error: " + errorObject.msg);
  53.             });
  54.         });
  55.         return deferred.promise;
  56.     },
  57.  
  58.     put: function (todos) {
  59.         WL.JSONStore.get(COLLECTION_NAME).clear();
  60.         WL.JSONStore.get(COLLECTION_NAME).add({data:JSON.stringify(todos)});
  61.     }
  62. };
  63.  
  64. }])
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top