Re: Using Angular $http service as documentLoader

On 06/18/2014 05:20 PM, Tomasz Pluskiewicz wrote:
> Hi
> 
> I'm trying to use the jsonld with Angular and in tests I want to
> replace the default documentLoader with $http service so that I can
> mock the responses.
> 
> I assumed that because $http already returns a promise it would be
> enough to simply use a function like
> 
> jsonld.documentLoader = function (url) {
>    return $http.get(url);
> }
> 
> as replacement for whatever is currently set.
> 
> Unfortunately it doesn't work. What's would be correct way to do that?

That's because the value that promise resolves to isn't what is required
by the JSON-LD API spec. It requires that the promise resolve to a
"RemoteDocument":

http://www.w3.org/TR/json-ld-api/#idl-def-RemoteDocument

I didn't test this, but it should be pretty close to what you want:

jsonld.documentLoader = function(url) {
  return $http.get(url).then(function(response) {
    return {
      contextUrl: null,
      document: response.data,
      documentUrl: url
    }
  });
};

Hopefully that works for you.

-Dave

-- 
Dave Longley
CTO
Digital Bazaar, Inc.

Received on Wednesday, 18 June 2014 23:10:40 UTC