Page 1 of 3

How to update Local Storage Array

Posted: Wed Aug 12, 2015 6:18 pm
by Randy7611273

Hello!

I have created a Generic Service which properly reads and filters a Local Storage Array. I cannot, however, seem to Update an item in said Array.

I would like for the user to click a button and change a Boolean value in the Array from “false” to “true.” I’ve tried dozens of methods, from modifying the JS code in the Generic Service to adding JS in the Service Request Mapping, but have not yet been able to make it work.

The item in the Array needing to be updated is votedReturn where it equals variable clickedSong. Again, the cdata.filter works beautifully: I just can’t update the related item.

Any assistance is greatly appreciated!

Thanks,
Randy


How to update Local Storage Array

Posted: Wed Aug 12, 2015 6:19 pm
by Randy7611273

Appery.localService2JS = Appery.createClass(null, {
init : function(requestOptions) {
this.requestOptions = $.extend({}, requestOptions);
},
process : function(settings) {
if (this.requestOptions.echo) {
settings.success(this.__requestOptions.echo);
} else {
var cdata = JSON.parse(localStorage.getItem("liveEventArray"));
var thisVote = (localStorage.getItem("clickedSong"));
var searched = thisVote;

Code: Select all

         [b]var voteReturn = (localStorage.setItem('votedReturn', ({ 
             thisVote: true})));[/b] 

         var match = cdata.filter(function(item) {  
             return item._id === searched;  
         });  
         settings.success(match);  
         settings.complete('success'); 
     } 
 }  

});


How to update Local Storage Array

Posted: Wed Aug 12, 2015 6:25 pm
by Randy7611273

How to update Local Storage Array

Posted: Thu Aug 13, 2015 6:02 pm
by Serhii Kulibaba

Hello Randy,

It is better to use storage variable instead of simple localStorage:
https://devcenter.appery.io/documenta...

it has update method


How to update Local Storage Array

Posted: Thu Aug 13, 2015 7:42 pm
by Randy7611273

I've reviewed the link, along with countless other posts... In my Generic Service I am trying several variations of the following, all of which are throwing errors:

Code: Select all

         var match = cdata.filter(function(item) {  
         [b] votedReturn.update({"$['thisVote']": true}); [/b] 
         return item._id === searched;  
          });  

I don't know either the proper syntax, or the location, for the needed code. To clarify: I need to change votedReturn to true where it equals item._id, which does return properly.


How to update Local Storage Array

Posted: Mon Aug 17, 2015 3:44 pm
by Serhii Kulibaba

You have to use JS like this:
preApperyio.storage.votedReturn.update("$['thisVote']", true);/pre

for setting storage variable's (votedReturn.thisVote) value = true


How to update Local Storage Array

Posted: Mon Aug 17, 2015 4:20 pm
by Randy7611273

Using that line of code returns the following error: "TypeError: Cannot read property 'update' of undefined at Appery.localService2JS.Appery.createClass.process"

Below is my complete Generic Service:

Appery.localService2JS = Appery.createClass(null, {
init : function(requestOptions) {
this.requestOptions = $.extend({}, requestOptions);
},
process : function(settings) {
if (this.requestOptions.echo) {
settings.success(this.__requestOptions.echo);
} else {
var cdata = JSON.parse(localStorage.getItem("liveEventArray"));
var thisVote = (localStorage.getItem("clickedSong"));
var searched = thisVote;

Code: Select all

         [b] Apperyio.storage.votedReturn.update("$['thisVote']", true);[/b] 

         var match = cdata.filter(function(item) {  
         return item._id === searched;                        
         });  

         settings.success(match);  
         settings.complete('success'); 
     } 
 }  

});


How to update Local Storage Array

Posted: Fri Aug 21, 2015 5:05 am
by Yurii Orishchuk

Hi Randy,

If following code:

pre

Apperyio.storage.votedReturn.update("$['thisVote']", true);

/pre

"votedReturn" is your storage name. If you have another storage name you should use your one.

Also "thisVote" is your storage attribute that you want to update.

If you will still have this problem please show us:

1 Your model("Storage and model" interface).
2 Your storage("Storage and model" interface).
3 Current storage value(you can use js code to get it in console):

pre

console.log(Apperyio.storage.votedReturn.get());

/pre

Regards.


How to update Local Storage Array

Posted: Mon Aug 24, 2015 2:27 pm
by Randy7611273

Hello Yurii,

I apologize if I am not using the proper terminology or syntax. Below are screenshots of my Model and Storage.

Using this code console.log(Apperyio.storage.votedReturn.get()); throws errors: TypeError: Cannot read property 'get' of undefined at Appery.localService2JS.Appery.createClass.process

I assume the errors are because votedReturn is contained in the Array. Using this code console.log(Apperyio.storage.clickedSong.get()); returns a proper, expected value.

To clarify: I am trying to set votedReturn to TRUE in Local Storage Array (liveEventArray) where _id = clickedSong.

I believe I’ve set up my Local Storage Array (liveEventArray) properly, as my filtering does work with queries: I just can’t get it to update.

Thanks!
Randy


How to update Local Storage Array

Posted: Mon Aug 24, 2015 2:28 pm
by Randy7611273