I’m working on an app where I want to provide an API for Ajax requests and I’m struggling to figure out the best way to go about it. My initial idea was to create a package named API under handlers and for every handler that I wanted to provide access to via Ajax I’d have an specific API handler and action like this:
// This is /api/account/list. Provides an API to the Account handler, list action
function list(event){
var rc = event.getCollection();
runEvent(“account.list”);
event.renderData(type = “json”, data = {
someData = rc.someData // the data I want to render
});
}
This would definitely work and is easy to implement but most every function would be very extremely similar I’d guess. Some functions might be a little different but mostly they’d be the same. Plus I’d have to manually create a bunch of API handlers. Not a huge deal. I’m just looking for perhaps a better way.
On the other hand, I’ve considered making some kind of generic API handler that would take a request like /api/account/list and run the account.list event automatically then convert the RC scope to JSON/XML for use. The problem I see here is that the RC scope contains a lot more than I need to actually return. Plus, it might contain complex entities that don’t always serialize well without a little massaging.
I’d accomplish this by adding a route:
addRoute(pattern="/api/:targetHandler/:targetAction",handler=“API”,action=“execute”);
And then having an API handler like this:
component{
function execute(event){
var rc = event.getCollection();
if(!structKeyExists(rc,“format”) || trim(rc.format) == “”){
throw(“Invalid format extension.”);
}
runEvent("#rc.targetHandler#.#rc.targetAction#");
event.renderData(type = rc.format, data = rc);
}
}
In the CB docs I did notice under the What’s New section this sample route which looks a lot like the second option I described. Is anyone doing something like generic API handler?
// route with custom constraints
addRoute(pattern="/api/:format/",
handler="api",
action="execute",
contraints={
format = "(xml|json)"
});
All this code is simplified and I’d have more error checking among other things but right now I’m just trying to figure out the best way to tackle this. So, all that said, how does everybody else handle this problem?