Page 1 of 2

edit data in success event of service

Posted: Tue May 07, 2013 2:23 pm
by w

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?


edit data in success event of service

Posted: Tue May 07, 2013 2:59 pm
by maxkatz

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


edit data in success event of service

Posted: Tue May 07, 2013 3:06 pm
by w

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?


edit data in success event of service

Posted: Tue May 07, 2013 5:51 pm
by maxkatz

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.


edit data in success event of service

Posted: Wed May 08, 2013 6:47 am
by w

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?


edit data in success event of service

Posted: Wed May 08, 2013 7:46 pm
by Sergey Kozyr

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


edit data in success event of service

Posted: Mon May 27, 2013 9:27 pm
by Andrés Méndez

Hello Sergey.

I receive this error:
this.settings is undefined

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


edit data in success event of service

Posted: Tue May 28, 2013 12:23 pm
by Kateryna Grynko

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.


edit data in success event of service

Posted: Tue May 28, 2013 7:15 pm
by Andrés Méndez

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.


edit data in success event of service

Posted: Tue May 28, 2013 9:06 pm
by Maryna Brodina

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