Page 1 of 1

Local Storage Variable

Posted: Thu Apr 24, 2014 3:20 am
by David Lim

I want to make a point system in a App by Using Local storage variable.
I can add and deduct point from the variable and upload to the database.
I not sure how


Local Storage Variable

Posted: Thu Apr 24, 2014 3:23 am
by Yan Yi

Refer to localstorage APIs

code
localStorage.setItem('text', input);
var text = localStorage.getItem('text');
localStorage.clear();
localStorage.removeItem("key");
/code


Local Storage Variable

Posted: Thu Apr 24, 2014 5:02 am
by Illya Stepanov

Hi David,

Ok, for your purpose we can suggest you the following easy solution:
ol
liCreate JS asset and insert the following code:
precode

//CollectionName - is name of the collection to be action with.
var addItemToCollection = function(action, collectionName, itemName, itemObject){
var collection = localStorage.getItem(collectionName);

Code: Select all

 if(!collection) 
     collection = "{}&quot 

 collection = JSON.parse(collection); 

 switch(action){ 
     case "set": 
         collection[itemName] = itemObject; 
         break; 

     case "remove": 
         delete collection[itemName]; 

         break; 

     case "get": 
         return collection[itemName]; 
         break; 

     case "getAll": 
         return collection; 
         break; 

 }; 

 localStorage.setItem(collectionName, JSON.stringify(collection)); 

};/code/pre/li

liTo set item for certain collection you should use the following code:
precode
//Where:
//"set" - method.
//"hello" - collection name.
//"myItem2" - itemName
//"myItemValue2" - itemValue
addItemToCollection("set", "hello", "myItem2", "myItemValue2");
/code/pre/li

liTo get item from certain collection you should use the following code:
precode
//Where:
//"get" - method.
//"hello" - collection name.
//"myItem2" - itemName
var itemValue = addItemToCollection("get", "hello", "myItem2");/code/pre/li

liTo remove item from certain collection you should use following code:
precode
//Where:
//"remove" - method.
//"hello" - collection name.
//"myItem2" - itemName
addItemToCollection("remove", "hello", "myItem2");/code/pre/li

liIf you want to get all collection object you should use following code:
precode
//Where:
//"remove" - method.
//"hello" - collection name.
var allCollectionObject = addItemToCollection("getAll", "hello");/code/pre/li

That's all regards./ol