<< Click to Display Table of Contents >> Navigation: Actions > Server API |
To define new server API
1. Create TWebUIAPI descendant class. Example:
TAddressAPI = class(TWebUIApi)
published
function ZIPtoAddress(const ZIP: string): TDMQuery;
end;
2. Implement published methods Example:
function TAddressAPI.ZIPtoAddress(const ZIP: string): TDMQuery;
begin
Result := Request.Provider.Q.SelectWP([ZIP], 'select * from address where zip=:1')
end;
Methods may return simple types, objects, records and arrays. Enumerable objects (lists, etc.) are also supported.
Note that if object should be destroyed after use, add it to free list by calling FreeafterUse.
3. Register class in server using RegisterAPI.
Server.RegisterAPI('address', TAddressAPI, alAuthorized);
API methods can be called from javascript via WebUI.Server object. Example:
WebUI.Server.address.ZIPtoAddress(this.value,
function(res){
form.City.value = res[0].city;
form.Country.value = res[0].city;
}
);
Since methods are executed asynchronously, last (additional) parameter is callback procedure taking result.
Depending on method result type, res may be sinple value or JSON object/array.