-1

I'm working on a AngularJS app. The app consumes a RESTful/HATEOAS API. The API has the following endpoint, amongst others:

http://localhost:8001/api/tests/1

which returns:

{
  "title": "Test",
  "description": "Test",
  "created": "2016-01-09T11:52:01+0100",
  "enabled": true,
  "is_speed_test": true,
  "test_id": 1,
  "view": [],
  "report_card": [],
  "cap": [],
  "tag": [],
  "_links": {
    "self": {
      "href": "http://localhost:8001/api/tests/2"
    },
    "module": {
      "href": "http://localhost:8001/api/subjects/2/modules/1"
    },
    "event": {
      "href": "http://localhost:8001/api/events/1"
    },
    "topics": {
      "href": "http://localhost:8001/api/subjects/2/modules/1/topics"
    },
    "creatorUser": {
      "href": "http://localhost:8001/api/users/admin"
    }
  }
}

How can I use Restangular to get a linked module, event, topics, etc. ?

Stalin Kay
  • 577
  • 8
  • 18

2 Answers2

0

Let's say you have your response in res object, you can parse your url with this code (found here, needed only if you use absolute urls):

var getLocation = function(href) {
  var l = document.createElement("a");
  l.href = href;
  return l;
};

then you can use Restangular with:

Restangular.one(getLocation(res._links.module).pathname).get();
Restangular.all(getLocation(res._links.topics).pathname).getList();
Restangular.all(getLocation(res._links.creatorUser).pathname).post(newUser);

Of course you still have to know which method to use and whether the response is a list or an object.

If the hostname is different from the one you configured Restangular to work with:

Restangular.withConfig(function(conf) {
  conf.setBaseUrl(getLocation(res._links.module).hostname);
}).one(getLocation(res._links.module).pathname).get();
Community
  • 1
  • 1
Mario Lamacchia
  • 1,703
  • 1
  • 13
  • 19
0

This is what I used to get it to work.

Restangular.oneUrl('module', test._link.module.href);
Stalin Kay
  • 577
  • 8
  • 18