laura6372139
Posts: 0
Joined: Wed Oct 30, 2013 11:54 am

favourite

I'm following these two links:

https://getsatisfaction.com/apperyio/...

https://getsatisfaction.com/apperyio/...

I want to save favorites in localStorage so that only the device.

First I put in local set storage variable "myFavorist"

Image

And then I create a Generic service:

Appery.prueba = Appery.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 { 
         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'); 
 } 

});

Image

And then load ivoke service.

But I get:

Image

This is my public link:

http://appery.io/app/mobile-frame?src...

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

favourite

Hi Laura,

How do you call the service? Where can we run it? Is the array 'myFavorits' empty?

laura6372139
Posts: 0
Joined: Wed Oct 30, 2013 11:54 am

favourite

The name service is "Generic Service".

Javascript is "favoritos"

And maybe that myFavorits array is empty.

I have a label "idfavoritos" connect with database:

Image

Image

And then save the label in localstorage "myFavorits"
Image

And then I create generic service.

Illya Stepanov
Posts: 0
Joined: Mon Mar 18, 2013 8:48 am

favourite

Hi Laura,

There is some mistakes in your app which you have to fix:
ol
liYour service called "GenericService" but in implementation you use "Appery.favoritos". You need to use "Appery.GenericService" instead./li

liIf you set local storage variable (LSV) this way: https://d2r1vs3d9006ap.cloudfront.net... in LSV will be stored just a value not a JSON.
/li/ol
So this code is not good:
precode

Code: Select all

 myFavorits = JSON.parse(localStorage.getItem("myFavorits")); 
 if ({}.toString.call(myFavorits) !== "[object Array]") { 
     myFavorits = []; 
 }/code/pre 

Please describe what this code should do.

Also please share your project with us, and describe steps to reproduce the problem.

Regards.

laura6372139
Posts: 0
Joined: Wed Oct 30, 2013 11:54 am

favourite

Hi Illya:

I change the fist step.

Now is:

Appery.GenericService

Image

LSV, I need that only read a value.

I change my code of GenericService but

Appery.GenericService = Appery.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 { 

         var cdata = localStorage.getItem("myFavorits"); 

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

});

Image

laura6372139
Posts: 0
Joined: Wed Oct 30, 2013 11:54 am

favourite

My app is share whit us.

The name is "City Up"

laura6372139
Posts: 0
Joined: Wed Oct 30, 2013 11:54 am

favourite

I need that when the users click on the picture on the page "Tienda" is added to the page "misfavoritos"

Igor
Posts: 0
Joined: Tue Apr 02, 2013 12:05 pm

favourite

Let's make you goals clear:

You have some list with image and "idfavoritos" label(with id of it item) inside each item.

You want when use click on image add this item to "myFavorits" localStorage variable(LSV).

Please try to follow below steps:

1 Open "mobilimage_43" click event handler. And change "myFavorits" to "myCurrentFavorite". http://prntscr.com/3ci1z8/direct

2 Add one another click event handler with action "Run Javascript" and fill it with following code: http://prntscr.com/3ci7y6/direct

pre

//Get current favorite(that's user clicked on).
var myCurrentFavorite = localStorage.getItem("myCurrentFavorite");

//Get stored favorites.
var myFavorits = localStorage.getItem("myFavorits");

//If myFavorits not exist - use simple array.
if(!myFavorits)
myFavorits = "[]";

//Parse it into the array
myFavorits = JSON.parse(myFavorits);

//Put myCurrentFavorite in the array.
myFavorits.push({id: myCurrentFavorite});

//Store modified array back to LSV.
localStorage.setItem("myFavorits", JSON.stringify(myFavorits));

//Check browser console to see what exactly is stored.
console.log(myFavorits);
/pre
3 Place your click event handler form step 2 into second position: http://prntscr.com/3ci8h7/direct

When user click on the image - id of this item will be added as new item into "myFavorits" LSV.

After that you can use this value like you need on other pages. To get current value in myFavorits LSV open console and use below code:
pre
console.log(localStorage.getItem("myFavorits"))
/pre

laura6372139
Posts: 0
Joined: Wed Oct 30, 2013 11:54 am

favourite

Hi Igor:

Thanks for your help!

I follow your steps:

Image

And then in my generic service:

Appery.GenericService = Appery.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 { 

         var cdata = console.log(localStorage.getItem("myFavorits")); 

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

});

But I get:

Image

Evgene Karachevtsev
Posts: 12
Joined: Mon Apr 28, 2014 1:12 pm

favourite

Hello Laura,

In file favoritos.js change "Appery.GenericService" to "Appery.favoritos"

Return to “Issues”