Adam Garbinski
Posts: 0
Joined: Sat Sep 28, 2013 5:33 pm

Generic Service implementation - how to filter local storage data?

Hello,

I am building GENERIC SERVICE for in my app, for OFFLINE MODE functionality. Basing on APPERY DOC and some general guidelines in this forum I have created actually pretty simple generic service, just to understand the basics of it.

I wanted this implementation to be like regular query REST service with WHERE parameter, so I could use it in OFFLINE MODE whenever I need to get specific data.
I used ARRAY FILTER which seemed the simplest approach and just for testing purposes I used singe database ID in variable SEARCHED.
Unfortunately I can't make it work as intended. It keeps returning all data instead of just the one item with that ID.

So I have two questions:

1) What is wrong with this ARRAY FILTER implementation?
2) How do I create Generic Service parameter, so it will appear in mapping UI?

This is the custom JS for Generic Service:

$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 { 
         var cdata = JSON.parse(localStorage.getItem("_Arttable_json_response")); 
         var searched = "52867b22e4b0be798fab779d";                
         var match = cdata.filter(function(item) { 
   return item._id === searched; 
}); 
         settings.success(cdata);             
         settings.success({}); 
         return match; 
     } 
     settings.complete('success'); 
 } 

});

This is how I pass data from database do local storage (on SUCCESS event):

var JSONresponse = JSON.stringify(data);
localStorage.setItem("_Arttable_json_response", JSONresponse);

This is my mapping:

Image

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

Generic Service implementation - how to filter local storage data?

Hello,

  1. When you test do you get any errors?
    You can add alert() or console.log() to debug you code.
  2. Please take a look at this tutorial http://docs.appery.io/tutorials/build...
Adam Garbinski
Posts: 0
Joined: Sat Sep 28, 2013 5:33 pm

Generic Service implementation - how to filter local storage data?

Hi Igor,

  1. No, I do not get any errors on the console. It just displays all items on the list control, instead of single one with id '52867b22e4b0be798fab779d'.
  2. Yes I know this one, but it deals with webSQL not with local storage and does not show how to set up service parameters.
    Can you give me some more hints on how to deal with these issues?
maxkatz
Posts: 0
Joined: Fri Aug 13, 2010 3:24 pm

Generic Service implementation - how to filter local storage data?

See here how to define parameters in Generic Service: https://getsatisfaction.com/apperyio/...

Adam Garbinski
Posts: 0
Joined: Sat Sep 28, 2013 5:33 pm

Generic Service implementation - how to filter local storage data?

Oops. Many thanks Max, for pointing this out, I indeed forgot about request parameters defined at UI thinking they have to be defined in JS.
I understand that I can call them inside custom JS for GENERIC SERVICE just by settings.data, for example if I request 'Name' parameter, then I will be able to call this parameter in my JS like that:

settings.data.Name

Right?

And do you have any suggestions regarding this "misbehaving" ARRAY FILTER?

Adam Garbinski
Posts: 0
Joined: Sat Sep 28, 2013 5:33 pm

Generic Service implementation - how to filter local storage data?

Hi
Any hope for answer to that?
Cheers,
Adam

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

Generic Service implementation - how to filter local storage data?

Are you referring to this:

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

Adam Garbinski
Posts: 0
Joined: Sat Sep 28, 2013 5:33 pm

Generic Service implementation - how to filter local storage data?

Yes Max, in case of code above it should theoretically return me only the one item since 'searched' is equal to '52867b22e4b0be798fab779d' id. But it returns a hefty list of items instead.

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

Generic Service implementation - how to filter local storage data?

What do you get in cdata?

Adam Garbinski
Posts: 0
Joined: Sat Sep 28, 2013 5:33 pm

Generic Service implementation - how to filter local storage data?

I get an array initiated by this code:

var cdata = JSON.parse(localStorage.getItem("_Arttable_json_response"));

It seems to be properly initiated because when I put this code inside filter function:

'console.log(item._id)'

then I get the ID-s of my items.

Return to “Issues”