Page 1 of 2

Generic Service implementation - how to filter local storage data?

Posted: Sat Nov 16, 2013 10:05 am
by Adam Garbinski

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


Generic Service implementation - how to filter local storage data?

Posted: Sat Nov 16, 2013 11:09 am
by Igor

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

Generic Service implementation - how to filter local storage data?

Posted: Sat Nov 16, 2013 1:51 pm
by Adam Garbinski

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?

Generic Service implementation - how to filter local storage data?

Posted: Sat Nov 16, 2013 3:50 pm
by maxkatz

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


Generic Service implementation - how to filter local storage data?

Posted: Sat Nov 16, 2013 4:13 pm
by Adam Garbinski

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?


Generic Service implementation - how to filter local storage data?

Posted: Sun Nov 17, 2013 1:34 pm
by Adam Garbinski

Hi
Any hope for answer to that?
Cheers,
Adam


Generic Service implementation - how to filter local storage data?

Posted: Sun Nov 17, 2013 3:41 pm
by maxkatz

Are you referring to this:

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


Generic Service implementation - how to filter local storage data?

Posted: Sun Nov 17, 2013 9:57 pm
by Adam Garbinski

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.


Generic Service implementation - how to filter local storage data?

Posted: Mon Nov 18, 2013 2:58 am
by maxkatz

What do you get in cdata?


Generic Service implementation - how to filter local storage data?

Posted: Mon Nov 18, 2013 6:55 am
by Adam Garbinski

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.