io-informatics / angular-jsonld

JSON-LD Client for AngularJS
32 stars 6 forks source link

Ease collection handling #2

Open astik opened 9 years ago

astik commented 9 years ago

I have a factory :

module.factory('siteService', function($q, $log, JsonldRest) {
    var api = {};

    JsonldRest.setBaseUrl('/api/v1/');
    var entitiesHandler = JsonldRest.collection('sites');

    api.findAll = function() {
        $log.log('siteService.findAll');
        return entitiesHandler.getList();
    };

    return api;
});

While using the findAll function, I get this error :

Error: Response for getList SHOULD be an array and not an object or something else

This is completely normal as the JSON is like :

{
  "@context":"/contexts/Site",
  "@id":"/sites",
  "@type":"hydra:PagedCollection",
  "hydra:totalItems":14,
  "hydra:itemsPerPage":30,
  "hydra:firstPage":"\/sites",
  "hydra:lastPage":"\/sites",
  "hydra:member":[
   {"@id":"/sites/1","@type":"Site","lang":"es"},
   {"@id":"/sites/2","@type":"Site","lang":"en"}
  ]
}

It is clear I need to work with "hydra:member". But I'd really like a simple way to manipulate the list.

@dunglas has a sweet approach to that : https://github.com/dunglas/DunglasJsonLdApiBundle#angularjs-integration

What I like is that we can manipulate directly the list (this is what is recommended by mgonto in its demo slide), but, in addition to that, we keep the pagination meta data.

I would be great to have this add-on directly into angular-jsonld in idea to have less code to bootstrap (I know, I'm lazy =P)

alexdeleon commented 9 years ago

Hi Romain, I have the code to handle hydra collections in another library. You are right, It makes sense to have here in angular-jsonld. I will put it up and let you know.

alexdeleon commented 9 years ago

Hi @astik , I still have to write the documentation, but if you want to try it the code is up. This is how you would use it:

entitiesHandler.getList('hydra:member');

You need to pass the member property (according to your local context). If you don't pass one it defaults to @graph.

You can check the unit test in:

https://github.com/io-informatics/angular-jsonld/blob/master/test/spec/services/jsonldRestSpec.js#L151

I hope it works for you. Let me know how it goes.

Best, alex

alexdeleon commented 9 years ago

by the way... you should be able to flow links to the pages. For instance:

var promiseToNextPage = entitiesHandler.getList('hydra:member').then(function(res){
   return res['hydra:nextPage'].get();
});