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

Generic Service implementation - how to filter local storage data?

Hello! Try this way:
pre$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(match);  
         settings.complete('success'); 
     } 
 }  

}); /pre

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

Generic Service implementation - how to filter local storage data?

Hello Maryna.

It is WORKING now! You've made my day!

So, for anyone, struggling with similar problem:

Assuming you have created service request parameter called '_id' and you would like your service named 'myGenericService' to return an object with particular id, then your custom JS should look like this:

$t.myGenericService = $t.createClass(null, {
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 match = cdata.filter(function(item) {
return item.id === settings.data._id;
});
settings.success(match);
settings.complete('success');
}
}
});

but most likely you would like to return objects containing specific substring in a key value, so assuming you have service request parameter called 'Name' then your code should look like this:

$t.myGenericService = $t.createClass(null, {
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 match = cdata.filter(function(item) {
return item.Name.indexOf(settings.data.Name) !== -1;
});
settings.success(match);
settings.complete('success');
}
}
});

Return to “Issues”