Angular - REST Service XML Links with $Resource

2019-08-10 04:55发布

I am using Angular with SilverStripe for my backend. I am using this module https://github.com/silverstripe/silverstripe-restfulserver for my REST API instead of building it myself.

When the API returns a list with a many_many relationship it looks like

<Recipe href="http://localhost/groceryList/api/v1/Recipe/2.xml">
<Title>Chilli</Title>
<ID>2</ID>
<Ingredients linktype="many_many" href="http://localhost/groceryList/api/v1/Recipe/2/Ingredients.xml">
    <Ingredient href="http://localhost/groceryList/api/v1/Ingredient/5.xml" id="5"/>
    <Ingredient href="http://localhost/groceryList/api/v1/Ingredient/6.xml" id="6"/>
    <Ingredient href="http://localhost/groceryList/api/v1/Ingredient/7.xml" id="7"/>
</Ingredients>
</Recipe>

My question is 2 part.

  1. Why is the collection of ingredients a bunch of links? I was hoping it would contain the name of each ingredient. Is this how REST is suppose to work?

  2. Using Angular $Resource, how can easily get the ingredients to loop through using ng-repeat?

1条回答
叼着烟拽天下
2楼-- · 2019-08-10 05:36

First you need to convert your XML to JSON with https://code.google.com/p/x2js/ or https://github.com/johngeorgewright/angular-xml, so you need an HttpInterceptor to convert all your requests and responses from the server.

I don't know why you don't have the name of each ingredient into your response, I think you need to change the response returned by your API, but I don't know how SilverStripe work.

查看更多
登录 后发表回答