Marc6388140
Posts: 0
Joined: Tue Nov 05, 2013 3:04 pm

How to add an item to "Favourites" locally in the device, not in the server?

I have managed to store an array of data in the same localStorage variable formatted this way: myFavorites: ["Aiguamúrcia", "Bràfim"].

Now, How can I retrieve these values in a list object?

Aiguamúrcia
Bràfim

I've tried with the action "Set property" for the text in the object at Load Event, but with no success...

Marc6388140
Posts: 0
Joined: Tue Nov 05, 2013 3:04 pm

How to add an item to "Favourites" locally in the device, not in the server?

So, how can I retrieve a list of all the values of a localStorage variable?

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

How to add an item to "Favourites" locally in the device, not in the server?

Hi Marc,

You can create Generic service and map its response to a list: https://getsatisfaction.com/apperyio/...

Marc6388140
Posts: 0
Joined: Tue Nov 05, 2013 3:04 pm

How to add an item to "Favourites" locally in the device, not in the server?

Sorry, but I'm completely lost. How can I make the Generic service read the array of values stored in the local variable myFavorits?

maxkatz
Posts: 0
Joined: Fri Aug 13, 2010 3:24 pm

How to add an item to "Favourites" locally in the device, not in the server?

The link that Katya posted above shows how to create a Generic Service that reads data from local storage and returns those values. You can use any JavaScript to read the data and format the data -- the idea behind the service is that you write the actual implementation.

Marc6388140
Posts: 0
Joined: Tue Nov 05, 2013 3:04 pm

How to add an item to "Favourites" locally in the device, not in the server?

OK, I have the localstorage variable myFavorits with these values: [{"MUNICIPI":"Valls"},{"MUNICIPI":"Bràfim"}], and I want to put these values in a list. To do that, as adviced, I've created a Generic Service and tried with this custom JS implementation:

$t.myimpl = $t.createClass(null, {

Code: Select all

 init: function(requestOptions) { 
     this.__requestOptions = $.extend({}, requestOptions); 
 }, 

 process: function(settings) { 
     if (this.__requestOptions.echo) { 
         settings.success(this.__requestOptions.echo); 
     } else { 
         console.log('Default implementation is used. Please define your own.'); 

         // save JSON data to local storage 
         // could be saved from a another (previous) service invocation. 
         var myFavorits; 
         myFavorits = JSON.parse(localStorage.getItem('myFavorits')) 
             localStorage.setItem("cacheddata", JSON.stringify(myFavorits)); 

         // load JSON data from local storage 
         var cdata = localStorage.getItem("cacheddata"); 

         // pass the JSON to the service, for output mapping 
         settings.success(JSON.parse(cdata)); 
     } 
     settings.complete('success'); 
 } 

});

Then I have added the GenericService as data Source and mapped as follows:

Image

The service is invoked at load and I geet the following console errors:

Image

Image

What am I doing wrong?

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

How to add an item to "Favourites" locally in the device, not in the server?

Hello! Are you doing this in shared ComarcApp app? You have mistake in service settings. In Implementation Name field there is Get_myFavorits, but Get_myFavorits.js first line is pre$t.myimpl = $t.createClass(null, {/preSo Implementation Name name should be myimpl instead Get_myFavorits. Also there seems to be some problem with JS in this service. You read data and save to localStorage, then read and save again (and you don't check data retrieved from localStorage). That's not right. Please try the following code:
pre$t.myimpl = $t.createClass(null, {
init: function(requestOptions) {
this.requestOptions = $.extend({}, requestOptions);
},
process: function(settings) {
if (this.requestOptions.echo) {
settings.success(this.__requestOptions.echo);
} else {
var myFavorits;
try {
myFavorits = JSON.parse(localStorage.getItem("myFavorits"));
if ({}.toString.call(myFavorits) !== "[object Array]") {
myFavorits = [];
}
} catch ( e ) {
myFavorits = [];
}
settings.success(myFavorits);
}
settings.complete('success');
}
});/pre

Marc6388140
Posts: 0
Joined: Tue Nov 05, 2013 3:04 pm

How to add an item to "Favourites" locally in the device, not in the server?

Yes, It works now! Again, Thank You very much for your support and patience.

Alex GG
Posts: 0
Joined: Thu Nov 14, 2013 11:11 pm

How to add an item to "Favourites" locally in the device, not in the server?

Hello Appery team!
I ́m also have an "add/remove from Favorites" functionallity in may app...I have follow this post and I ́m able to add items to the favorits list. And then, using Generic Service, to retrieve the items to another list.
this is what I want to do:
In order to avoid user add same item multiple times in the favorite list I want to dinamically change the icon using this:
Appery('Image_favorits').attr('src', Appery.getImagePath('addfavorits.jpg'));
or if this is the case,
Appery('Image_favorits').attr('src', Appery.getImagePath('removefavorits.jpg'));

Image

so, if the user decides to remove from favorits list, he just click "X"...

I also look at this past post to remove items from an Array:
https://getsatisfaction.com/apperyio/...

using this on button click:
removeArrayItem('favorites',Appery('negocioo').text());

and adding this custom JS:

function save(item, varName) {
var arr = localStorage.getItem(varName);
if(arr == null) {
arr = new Array();
}
else {
arr = JSON.parse(arr);
}
arr.push(item);
localStorage.setItem(varName, JSON.stringify(arr));
}

function myload(varName) {
var arr = localStorage.getItem(varName);
if(arr == null) {
arr = new Array();
}
else {
arr = JSON.parse(arr);
}
return arr;
}

function removeArrayItem(varName, itemValue) {
var arr = myload(varName);
if(arr.indexOf(itemValue) != -1) {
arr.splice(arr.indexOf(itemvalue));
localStorage.setItem(varName, JSON.stringify(arr));
}
}

But I ́m not able to remove any item..I ́m lost...Could you point me on how to do the trick???

BTW this is what I have in "favorits" local Variable:

Image

Regards

Return to “Issues”