I have a Spring HATEOAS restful api to provide datas for my polymer front-end.
Data is received like this:
GET /api/tips/news :
[ {
"id" : 68,
"content" : "example tip",
"score" : 1,
"creationDate" : 1456257119018,
"links" : [ {
"rel" : "self",
"href" : "http://localhost:81/api/tip/68"
}, {
"rel" : "author",
"href" : "http://localhost:81/api/user/59"
}, {
"rel" : "for",
"href" : "http://localhost:81/api/class/65"
}, {
"rel" : "against",
"href" : "http://localhost:81/api/class/66"
}, {
"rel" : "comments",
"href" : "http://localhost:81/api/tip/68/comments"
} ]
} ]
Problem is that I want to get author
property from links, so I want to do a second request once the first one is complete. To do this, I have a component that includes an iron-ajax
component, to do the first api with my api Key and all the security stuff.
Here is how I do the first api call:
<bnstips-api uri="/tip/news" result="{{data}}" auto></bnstips-api>
<iron-list items="[[data]]" as="item">
<template>
<tip-card tip=[[item]]></tip-card>
</template>
</iron-list>
and here is the tip-card
component:
<dom-module id="tip-card">
<style>
</style>
<template>
<paper-card>
<div class="card-content">
[[tip.content]]
Author:<bnstips-api api-path="[[tip.links.1.href]]" result={{author}} auto></bnstips-api> [[author.name]] [[tip.links.1.href]]
</div>
<div class="card-actions">
<paper-button>Upvote !</paper-button>
</div>
</paper-card>
</template>
<script>
Polymer({
is: "tip-card",
properties: {
tip: {
type: Object,
value: {}
}
}
});
</script>
</dom-module>
the api-path property is used to provide full path instead of just uri, here is the logic behind (in bnstips-api):
if(this.uri != "" || this.uri != undefined){
this.url = basePath+this.uri+"?apiKey="+apiKey;
}
if(this.apiPath != ""){
this.url = this.apiPath+"?apiKey="+apiKey;
}
But I get this:
So the link is provided, but the second ajax request gets a wrong uri, because in the console, I can see that I get a request to http://localhost:81/undefined?apiKey.....
even if the property is here.
How to avoid that?
EDIT:
I tried to console.log(this.apiPath)
inside my handleRequest()
method, and it shows the good value, seems like it's an asynchronous problem (second ajax request is sent before the first one actually ends, so the second one has an undefined path since it has to come from the first one. How to wait for the first one to finish before sending the second one?