Page 2 of 3

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

Posted: Mon Nov 18, 2013 1:18 pm
by Marc6388140

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...


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

Posted: Mon Nov 18, 2013 4:43 pm
by Marc6388140

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


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

Posted: Mon Nov 18, 2013 8:03 pm
by Kateryna Grynko

Hi Marc,

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


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

Posted: Wed Nov 20, 2013 7:48 pm
by Marc6388140

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


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

Posted: Wed Nov 20, 2013 8:06 pm
by maxkatz

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.


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

Posted: Thu Nov 21, 2013 10:36 pm
by Marc6388140

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?


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

Posted: Fri Nov 22, 2013 9:20 am
by Maryna Brodina

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


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

Posted: Fri Nov 22, 2013 1:49 pm
by Marc6388140

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


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

Posted: Fri Jan 24, 2014 4:48 am
by Alex GG

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


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

Posted: Fri Jan 24, 2014 1:52 pm
by Kateryna Grynko

Please try debugging.
http://docs.appery.io/documentation/d...
Are there any console errors?