Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- factory('todoJsonStorage',['$q', function ($q) {
- 'use strict';
- var COLLECTION_NAME = 'Users';
- var collections = {
- Users: {
- searchFields: {UserId: 'string', password: 'string'}
- },
- };
- var options = {};
- //Optional username
- options.username = 'testuser';
- //Optional password
- options.password = 'test123';
- //Optional local key generation flag
- options.localKeyGen = true;
- var inited = false;
- //checks if inited and if not inits
- function initJSONStore(){
- var initDeferred = $q.defer();
- if (inited){
- initDeferred.resolve();
- } else {
- //Initialize the collection
- WL.JSONStore.init(collections,options).then(function () {
- console.log("-> JSONStore init successful");
- initDeferred.resolve();
- }).fail(function (errorObject) {
- console.log("-> JSONStore error: " + errorObject.msg);
- });
- return initDeferred.promise;
- };
- }
- return {
- get: function () {
- var deferred = $q.defer();
- initJSONStore().then(function(){
- WL.JSONStore.get(COLLECTION_NAME).findAll().then(function (res) {
- if (res.length > 0){
- deferred.resolve(JSON.parse(res[0].json.data || '[]'));
- } else {
- deferred.resolve(res);
- }
- }).fail(function (errorObject) {
- console.log("JSONStore findbyid error: " + errorObject.msg);
- });
- });
- return deferred.promise;
- },
- put: function (todos) {
- WL.JSONStore.get(COLLECTION_NAME).clear();
- WL.JSONStore.get(COLLECTION_NAME).add({data:JSON.stringify(todos)});
- }
- };
- }])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement