w
Posts: 0
Joined: Tue Mar 26, 2013 12:30 pm

edit data in success event of service

Is it possible to edit the data object that is available in the success event of a service? For example to sort the JSON data inside the data object before it is then mapped to the UI elements?

I read about how you can to this with a generic service,but is it really necessary to have 1 service, and to create a 2nd service just to sort the results of the 1st service?

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

edit data in success event of service

You get the data returned from the service into the 'complete' event. Check out jQuery docs and this http://docs.appery.io/documentation/a...

w
Posts: 0
Joined: Tue Mar 26, 2013 12:30 pm

edit data in success event of service

Thank you, but how do I sort the data in this complete event? There is a jqXHR object available, but is this a reference and can I change the responsedata that is inside that object, and will these changes be reflected in the mapping to the UI?

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

edit data in success event of service

Success and Complete are fired after the mapping.

One option is to create a Generic Service where you would make an Ajax call to the service, get the data back and manipulate the data as needed.

Another option is to update the service to return already sorted data.

w
Posts: 0
Joined: Tue Mar 26, 2013 12:30 pm

edit data in success event of service

Could you give an example on how to do the Ajax call in the generic service, and how to receive the data and the return them as the generic service output?

Sergey Kozyr
Posts: 0
Joined: Tue Apr 30, 2013 2:55 pm

edit data in success event of service

To create generic rest service click Create New Service and select "Generic" type.
Image

Now click "Add custom implementation":
Image
You will be asked to enter javascript implementation name. Let's write "customTwitterSearch".
New Javascript asset is created. It must be changed to:
precode

$t.customTwitterSearch = $t.createClass(null, {

Code: Select all

 init : function(requestOptions) { 
     this.__requestOptions = $.extend({}, requestOptions); 
 }, 

 process : function(settings) { 
     this.settings = settings; 

     if (this.__requestOptions.echo) { 
         settings.success(this.__requestOptions.echo); 
     } else { 
         $.ajax({ 
             url: "http://search.twitter.com/search.json?q=help", 
             context: this, 
             dataType: "jsonp" 
         }).done(this.onAjaxDone).error(this.onAjaxError); 
         showSpinner(); 
     } 
 }, 

 onAjaxDone: function(data) { 
     //You can filter "data" here 
     this.settings.success(data); 
     this.settings.complete('success'); 
     hideSpinner(); 
 }, 

 onAjaxError: function(jqXHR, textStatus, errorThrown) { 
     this.settings.error(jqXHR, textStatus, errorThrown); 
     this.settings.complete('error'); 
     hideSpinner(); 
 } 

});
/code/pre
(here is same code with syntax highlighting)
In this implementation AJAX request is made to search tweets. In the function "onAjaxDone" you may write any code to change "data" variable. After modification of ajax retrieved response is passed to Appery as response data at line:
codethis.settings.success(data);/code

Andrés Méndez
Posts: 0
Joined: Thu Apr 18, 2013 6:13 pm

edit data in success event of service

Hello Sergey.

I receive this error:
this.settings is undefined

Line 34: this.settings.error(jqXHR, textStatus, errorThrown);

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

edit data in success event of service

Hi Andrés,

Generic Service works for us. The error seems to be in your JavaScript code. Please send us the developed Generic service source code.

Andrés Méndez
Posts: 0
Joined: Thu Apr 18, 2013 6:13 pm

edit data in success event of service

Hello Katya.

After debugging and testing, I have seen that the problem is that the REST request isn't working correctly, so it goes to the error part of the script, which doesn't seem to work.

To make the REST service work in Appery, I have to check the "Use Appery Proxy", and I don't know how to program it inside the code. How can I use the Appery proxy?

Thanks.

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

edit data in success event of service

Hi, no news for now. I'll update after get any information.

Return to “Issues”