jcassee / angular-hypermedia

Hypermedia REST API client for AngularJS applications
MIT License
38 stars 5 forks source link

Complete Example #29

Open robopeter opened 8 years ago

robopeter commented 8 years ago

Could a complete working example be provided to augment the snippets of examples provided? Specifically I'd really appreciate an example that shows loading a JSON HAL collection into an array of models/objects. Even more specifically I have a JSON file like this that I'd like to create an array of article objects from:

{

  "_links": {
    "self": {
      "href": "/articles/"
    }
  },
  "count": 2,
  "_embedded": {
    "articles": [
      {
        "_links": {
          "self": "/articles/2",
          "collection": "/articles/"
        },
        "size": 7633,
        "comments": [
          "/comments/88",
          "/comments/76",
          "/comments/79",
          "/comments/90",
          "/comments/78",
          "/comments/91"
        ],
        "title": "foo",
        "sha256": "e76a675caadef4d148866d063005550252528792d658cb270bcd1ea287f17261",
        "id": 2
      },
      {
        "_links": {
          "self": "/articles/3",
          "collection": "/articles/"
        },
        "size": 24650,
        "comments": [
          "/comments/100",
          "/comments/94",
          "/comments/93"
        ],
        "title": "bar",
        "sha256": "7b98258260d69da3937342846dc59b4769dda38a49f92a9bf42c302b97299220",
        "id": 3
      }
    ]
  }
}

Otherwise, could complete JSON be provided along side the corresponding existing code examples? Thanks!

jcassee commented 8 years ago

A complete example would be useful, I agree. I'll see if I can add one.

In the mean time, this should do the trick in your case: (not verified, may contain typos)

var context = new ResourceContext(HalResource);
var collection = context.get(URI);
collection.$get().then(function () {
  var articles = collection.$linkRel('articles');
  // Do something with articles
});
robopeter commented 8 years ago

Ok, I think I got things working. I had several problems, several of which were really angular problems vs this library directly! The two calls to "get" above were non-obvious at first glance.

It would also probably be good to illustrate the differences between $propRel vs $linkRel vs $loadPaths complete with the corresponding JSON for when you'd use each. Thanks again!

xskid commented 6 years ago

@jcassee ,

I am reinforcing the @robopeter needs.

It is being complicated to understand all the options available.

For instance, when should i use $propRel, $linkRel, $loadPaths