etcgroup / text-prizm

3 stars 0 forks source link

Client-side API URL Wrapper #67

Open geosoco opened 11 years ago

geosoco commented 11 years ago

The goal here is to create a Javascript wrapper class to hold all of the API URLs. This creates a central store for all of the API calls such that any changes can be changed in a single place.

Ideally, these would be housed under a javascript closure to imitate namespaces and help keep the calls organized in a hierarchy. Something like:

function Ext() { return { "InstanceCountsByUser" : "/api/ext/summary_info/user/applied_instances", "InstancesOfCode": "/api/ext/summary_info/code/instances", } }

michaelbrooks commented 11 years ago

Looks good. Here are my suggestions.

First, add a "api.js" file to src/client/js. Build an object literal like you suggested, but you won't have to enclose it that way. Look at the helpers in src/client/js/common/helpers.

Put a define(...) call around your object definition and make sure to return the object at the end. You probably won't have anything to include at the top, of course. This will keep it from interfering with the global namespace and allow us to include it into our model files.

define(function() {
   var Api = {
      ...
   };
   return Api;
});

A use case (not real):

var CodeModel = Backbone.Model.extend({
   url: Api.Ext.InstanceCountsByUser
});

I'd suggest using a little bit of structure to divide up the API at the top (i.e. REST and Ext or something), but the names should be long and verbose, describing exactly what you are getting back. They probably shouldn't mirror the url structure, since that would defeat the purpose.