indexzero
8/1/2011 - 6:43 AM

An example of a possible "resource" API in node.js

An example of a possible "resource" API in node.js

var util = require('util'),
    resourceLib = require('our-new-resource-lib');

var SomeResource = function (options) {
  resourceLib.Resource.call(this, options);

  //
  // Setup any other application specific state.
  //
  
  //
  // Require authorization for GET, PUT, POST, and DELETE
  //
  this.authorize('GET', 'PUT', 'POST', 'DELETE')
  
  //
  // Setup this resource to use CouchDB. 
  //
  this.use('couch')
};

util.inherits(SomeResource, resourceLib.Resource);

SomeResource.prototype.get = function (params, callback) {
  var self = this;
  //
  // Here you would access the super "classes" `._get()` method 
  // and perform any default translations
  //
  this._get(params.name, function (err, instance) {
    //
    // Do something like map the instance properties, etc., 
    // then invoke the callback via the render function
    //
    self.render(instance, callback);
  });
};