W3C home > Mailing lists > Public > public-linked-json@w3.org > June 2014

Re: Using Angular $http service as documentLoader

From: Tomasz Pluskiewicz <tomasz@t-code.pl>
Date: Thu, 19 Jun 2014 13:23:11 +0200
Message-ID: <CAAFOhSfiHz0kz4RkiK-SuNBHwS+XsmvSBEv5s7GZQMf71npkyQ@mail.gmail.com>
To: Dave Longley <dlongley@digitalbazaar.com>
Cc: public-linked-json@w3.org, tomasz@t-code.pl
On Thu, Jun 19, 2014 at 1:10 AM, Dave Longley
<dlongley@digitalbazaar.com> wrote:
> 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.
>

Unfortunately it's not that easy. I'm not very experienced with
JavaScript or Angular. I created a fiddle [1] similar to my failing
code.

As is, XHR fails, because it's a CORS request. If you uncomment the
two lines two weird things happen (or don't happen). First, $http mock
fails saying there aren't any pending requests and none of the
console.log called in callbacks are invoked. Which is weird, because
it means that even the $http promise is never resolved. I tried to
debug it but I get lost somewhere inside jsonld/angular. Also there
isn't any other exception being caught bu Chrome dev tools.

Any ideas? Hasn't someone already done that?

Thanks,
Tom

[1] http://jsfiddle.net/tpluscode/R5acr/12/

>
> -Dave
>
> --
> Dave Longley
> CTO
> Digital Bazaar, Inc.
>
Received on Thursday, 19 June 2014 11:24:46 UTC

This archive was generated by hypermail 2.4.0 : Friday, 17 January 2020 16:18:41 UTC