- First i did create a ContactRESTService adapter
- Then i changed the ContactRESTService-impl.js like this
My adapter has 2 methods first isfunction getContactList() { var input = { method : 'get', returnedContentType : 'xml', path : '/ManageContact/rest/contact' }; return WL.Server.invokeHttp(input); } function searchContact(lastName){ var input = { method : 'get', returnedContentType : 'xml', path : '/ManageContact/rest/contact/search?lastName='+lastName }; return WL.Server.invokeHttp(input); } function getStoriesFiltered(){ }
getContactList()
that returns all the contacts in the database by calling/ManageContact/rest/contact
URL and it does not take any parameter. ThesearchContact()
takes lastName as parameter and makes GET call to'/ManageContact/rest/contact/search?lastName='+lastName
URL. -
I also had to change the ContactRESTService.xml so that my adapter descriptor looks like this
<?xml version="1.0" encoding="UTF-8"?> <wl:adapter name="ContactRESTService" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:wl="http://www.worklight.com/integration" xmlns:http="http://www.worklight.com/integration/http"> <displayName>ContactRESTService</displayName> <description>ContactRESTService</description> <connectivity> <connectionPolicy xsi:type="http:HTTPConnectionPolicyType"> <protocol>http</protocol> <domain>localhost</domain> <port>9000</port> </connectionPolicy> <loadConstraints maxConcurrentConnectionsPerNode="2" /> </connectivity> <procedure name="getContactList"/> <procedure name="searchContact"/> <procedure name="getStoriesFiltered"/> </wl:adapter>
-
After that i had to make couple of my minor change in my WorkLight application so that it would use the HTTP adapter instead of the SQL adapter, This is how my JavaScript that makes call to the adapter looks like
First thefunction getContact(){ console.log("Entering getContact() REST service based version"); var contactName = $('contactName').getValue(); var invocationData = { adapter:"ContactRESTService", procedure:"searchContact", parameters:[contactName] } var options ={ onSuccess:loadContactSuccess, onFailure:loadContactFailure } WL.Client.invokeProcedure(invocationData, options); } function loadContactSuccess(result){ console.log("Inside loadContactSuccess " + result); var html = ''; if(result.invocationResult.isSuccessful){ var contactList = result.invocationResult.contacts.contact; var i = 0; for(i =0 ; i < contactList.length ; i++){ var currentContact = contactList[i]; html = html + '<li><a href="#">'+currentContact.firstName +' ' +currentContact.lastName +'</a></li>'; } } jq("#displayContact").html(html); jq("#displayContact").listview('refresh'); } function loadContactFailure(result){ console.log("Inside loadContactError " + result); }
getContact()
which makes call to the HttpAdapter had to changed to use the name of theContactRESTService
as adapter andsearchContact
as procedure name. Then i had to change the loadContactSuccess() method the part which reads the search results. Worklight makes sure that i get results in JSON format with little bit different structure.
Invoking REST service from WorkLight
In the Using JQuery Mobile in WorkLight application entry i blogged about how to create a simple Contact Search application that takes user's last name as parameter and searches all the contacts with that name displays to the user. In that example i used the WorkLight SQL adapter for making query, but i already have my own REST service that can do same thing and i wanted to use it.
This service takes part of last name as query parameter and returns all the contacts that match the name in XML format. I wanted to use this service in my WorkLight application, so that i could use the Http Adapter so i followed these steps
No comments:
Post a Comment