Question

I've got an external RESTful endpoint that I'm trying to build a model off of in my CompoundJS app. I'm a bit confused about a couple of things:

  1. How do I build a model off of an external RESTful API (most models seem to be database focused)?
  2. What's the best way to access this RESTful API?

Right now, I'm doing this:

var options = {
  host: 'api.local',
  port: 80,
  path: '/users',
  method: 'GET',
  headers: {
    'Content-Type': 'application/json'
  }
};

var http = require('http');
http.get(options, function(res) {
  var output = "";
  res.on('data', function(chunk) {
    output += chunk;
  });

  res.on('end', function() {
    var obj = JSON.parse(output);
    send({code: 200, data: obj});
  });
});

obj does actually return with all of my user data (but in JSON). The JSON returned looks like this:

{
  time: integer,
  count: integer,
  data: [{
    userid: string,
    name: string,
    email: string
  }, {
    userid: string,
    ...
  }]
}

JSON's great and all, but I'd love to load it into a model, so I'm thinking I would have an outer model that contains time (with type integer), count (with type integer), and data (with type array). Then I have an inner model (let's call it User) that contains userid (with type string), name (with type string), and email (with type string). I would then have to associate data with having many User models. I'm hoping for something along the lines of how Ext JS does it.

So how do I build a model based on this RESTful endpoint's return, and what's the best way of getting this data from the endpoint? Is my current code above the best route for grabbing data, or can I set up a cleaner approach using the model I create in Schema.js? Ideally, I'd be able to create a model in Schema.js, hook that with the RESTful API, and just do normal GET/POST operations.

Any help would be appreciated!

Was it helpful?

Solution

Current working solution that I came up with... seems to work, but it looks a bit hacky. If you see the TODO comment, that is what I'd love to be able to do... I'm just wondering if it's possible at this point. This allows me to control what the user model shows/hides, so I'm a bit happier.

Schema.js

var User = describe('User', function() {
  property('userid', String);
  property('name', String);
  property('email', String);
  set('restPath', pathTo.users);
});

var Hash = describe('Hash', function () {
    property('time', Number);
    property('count', Number);
    property('data', new Array());
    set('restPath', pathTo.hashes);
});

hashes_controller.js

var options = {
  host: 'api.local',
  port: 80,
  path: '/users',
  method: 'GET',
  headers: {
    'Content-Type': 'application/json'
  }
};

var http = require('http');
http.get(options, function(res) {
  var output = "";
  res.on('data', function(chunk) {
    output += chunk;
  });

  res.on('end', function() {
    var obj = JSON.parse(output);
    var data = {};
    data.msec = obj.msec;
    data.count = obj.count;
    data.users = [];
    // TODO: Ideally, this is what I would like to do
    // var hash = new Hash(obj);
    for (var i = 0; i < obj.data.length; i++) {
      var user = new User(obj.data[i]);
      data.users.push(user);
    }
    send({code: 200, data: data});
  });
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top