Code Monkey home page Code Monkey logo

Comments (19)

guylabs avatar guylabs commented on May 28, 2024

Hi Romy,

so you want to call the adapter the following way:

SpringDataRestAdapter.process(response, '_allLinks')

But in your data model you have links which point to entities from which they are linked from and thus causing a loop in the adapter. What you can do is to define which links you want to automatically resolve with the following usage:

SpringDataRestAdapter.process(response, ['anotherLink', 'testLink'])

Please also have a look at the documentation here: https://github.com/guylabs/angular-spring-data-rest#fetch-multiple-or-all-links

I hope that answers your two questions.

Thanks and regards,

Guy

from angular-spring-data-rest.

rkusuma avatar rkusuma commented on May 28, 2024

Thanks for your response @guylabs

SpringDataRestAdapter.process(response, ['anotherLink', 'testLink'])

I mean if anotherLink have anotherAnotherLink, can i get anotherAnotherLink automatically?

from angular-spring-data-rest.

guylabs avatar guylabs commented on May 28, 2024

Well you would need to define all the links you want to resolve in this list. So if you want to resolve the anotherAnotherLink then you need to call it this way:

SpringDataRestAdapter.process(response, ['anotherLink', 'testLink', 'anotherAnotherLink'])

What is your use case and how is your data model such that you get an infinite loop ?

from angular-spring-data-rest.

rkusuma avatar rkusuma commented on May 28, 2024

I have response JSON like this:

{
    "_links": {
        "self": {
            "href": "http://localhost:8080/api/reports"
        }
    },
    "_embedded": {
        "reports": [
            {
              "reportNumber": "00001",
              "reportedBy": "Xxx",
              "_links": {
                "self": {
                  "href": "http://localhost:8080/api/reports/00001"
                },
                "city": {
                  "href": "http://localhost:8080/api/reports/00001/city"
                },
                "reportStatus": {
                  "href": "http://localhost:8080/api/reports/00001/reportStatus"
                },
                "reportDates": {
                  "href": "http://localhost:8080/api/reports/00001/reportDates"
                },
                "accident": {
                  "href": "http://localhost:8080/api/reports/00001/accident"
                }
              }
            }
        ]
    },
    "page": {
        "size": 20,
        "totalElements": 4,
        "totalPages": 1,
        "number": 0
    }
}

And i need to fetch all links recursively.

  1. If reportDates have more 2 links, how can i get them?
  2. In accident, there is link to report.
    Here is response from accident:
{
    "accidentDate": "2015-07-05",
    "accidentLocation": "In Main Street",
    "_links": {
        "self": {
            "href": "http://localhost:8080/api/accidents/00001"
        },
        "city": {
            "href": "http://localhost:8080/api/accidents/00001/city"
        },
        "report": {
            "href": "http://localhost:8080/api/accidents/00001/report"
        }
    }
}

fetching report will make infinite loop, because report have links accident and accident have links to report.

Thanks for your response. 👍

from angular-spring-data-rest.

guylabs avatar guylabs commented on May 28, 2024

Ok I see the problem. Well you could implement an own fetch function which deals with this situation: https://github.com/guylabs/angular-spring-data-rest#exchange-the-underlying-fetch-function.

Because implementing a logic for that is not that trivial, as you would need to have a structure where you would store all links and which links are referencing other links and therefore it would consume memory just for avoiding infinite loops. Do you know what I mean?

Another solution would be to add a level option where you could define how much levels it should recursively resolve such that you do not get an infinite loop.

I think the second solution would be easier and doesn't complicate the whole codebase.

What do you think?

Thanks and regards,

Guy

from angular-spring-data-rest.

rkusuma avatar rkusuma commented on May 28, 2024

I see..
so what you mean we must implement new fetchFunction with level option ?
I think it still not solve the problem, because if i have to avoid infinite loop with 2 level recursive and i need another link fetch rescursively until 4 level, then i will not get all the fetched data.

How about you add param array of ignoredFetchLinkNames, and while fetching the links it will skip the links in ignoredFetchLinkNames.
What do you think?

Thanks @guylabs

from angular-spring-data-rest.

guylabs avatar guylabs commented on May 28, 2024

But then you would have the same problem that when you need the link fetched once you are not able to fetch it.

One possibility would be to create a map which has the self link as key and a list of linkNames which were already fetched for that self key here: https://github.com/guylabs/angular-spring-data-rest/blob/master/src/angular-spring-data-rest-provider.js#L257

Then here https://github.com/guylabs/angular-spring-data-rest/blob/master/src/angular-spring-data-rest-provider.js#L252 we would check if the link to be fetched is already in the list and then skip the fetching.

This way you could use the recursive fetching and it would just fetch each link once.

I think this would be the proper solution. What do you think?

Regards,

Guy

from angular-spring-data-rest.

rkusuma avatar rkusuma commented on May 28, 2024

I think that's a good idea @guylabs !
So we can use recursive without thinking about infinite loop.

when you would implement that idea ?

from angular-spring-data-rest.

guylabs avatar guylabs commented on May 28, 2024

Hi Romy,

well that is the other problem. As I do not have much time and I also have some other open source projects I need to work on I cannot tell you. But you could come up with a solution with some tests and create a pull request if you need the feature in the near future.

Thanks and regards,

Guy

from angular-spring-data-rest.

rkusuma avatar rkusuma commented on May 28, 2024

@guylabs

SpringDataRestAdapter.process(response,  ['anotherLink', 'testLink']).then(function(processedResponse) {
  ...
});

Can i use recursive in that code example?

SpringDataRestAdapter.process(response,  ['anotherLink', 'testLink'], true).then(function(processedResponse) {
  ...
});

from angular-spring-data-rest.

guylabs avatar guylabs commented on May 28, 2024

Yes this should also work, so you tell the adapter to recursively process the anotherLink and testLink. So does this solves your problem?

from angular-spring-data-rest.

rkusuma avatar rkusuma commented on May 28, 2024

@guylabs yes.. that solve one of my problem..
now only infinite loop problem..

from angular-spring-data-rest.

guylabs avatar guylabs commented on May 28, 2024

Pull request #15 has been integrated.

from angular-spring-data-rest.

rkusuma avatar rkusuma commented on May 28, 2024

Hi @guylabs,

How to reinitialized the linkMap for each item in _embeddedItems ?
For example:

{
    "_links": {
        "self": {
            "href": "http://localhost:8080/api/reports"
        }
    },
    "_embedded": {
        "reports": [
            {
              "reportNumber": "00001",
              "_links": {
                "self": {
                  "href": "http://localhost:8080/api/reports/00001"
                },
                "city": {
                  "href": "http://localhost:8080/api/reports/00001/city"
                },
                "accident": {
                  "href": "http://localhost:8080/api/reports/00001/accident"
                }
              }
            },
            {
              "reportNumber": "00002",
              "_links": {
                "self": {
                  "href": "http://localhost:8080/api/reports/00002"
                },
                "city": {
                  "href": "http://localhost:8080/api/reports/00002/city"
                },
                "accident": {
                  "href": "http://localhost:8080/api/reports/00002/accident"
                }
              }
            }
        ]
    },
    "page": {
        "size": 20,
        "totalElements": 4,
        "totalPages": 1,
        "number": 0
    }
}

Should each item have their own linkMap ?

from angular-spring-data-rest.

guylabs avatar guylabs commented on May 28, 2024

Hi Romy,

so you want to move the line https://github.com/guylabs/angular-spring-data-rest/blob/master/src/angular-spring-data-rest-provider.js#L370 to here https://github.com/guylabs/angular-spring-data-rest/blob/master/src/angular-spring-data-rest-provider.js#L136 right?

Well I think that would be a good idea, as I think this would be the correct way of dealing with this map. I can do that when I have more time than now or may you want to create a pull request for this small change such that you can use it in the master?

Thanks and regards,

Guy

from angular-spring-data-rest.

rkusuma avatar rkusuma commented on May 28, 2024

I think if we move it like you said, then it will cause infinite loop again, every time we call function processData the linkMap is empty again.

CMIIW

from angular-spring-data-rest.

guylabs avatar guylabs commented on May 28, 2024

Yes you are right I just overflew it briefly. Well then we need to leave it as it is right now or is there still an issue?

from angular-spring-data-rest.

rkusuma avatar rkusuma commented on May 28, 2024

Hi Guy,

Yes there still an issue.
API return like this:

{
    "_links": {
        "self": {
            "href": "http://localhost:8080/api/reports"
        }
    },
    "_embedded": {
        "reports": [
            {
              "reportNumber": "00001",
              "_links": {
                "self": {
                  "href": "http://localhost:8080/api/reports/00001"
                },
                "city": {
                  "href": "http://localhost:8080/api/reports/00001/city"
                },
                "accident": {
                  "href": "http://localhost:8080/api/reports/00001/accident"
                }
              }
            },
            {
              "reportNumber": "00002",
              "_links": {
                "self": {
                  "href": "http://localhost:8080/api/reports/00002"
                },
                "city": {
                  "href": "http://localhost:8080/api/reports/00002/city"
                },
                "accident": {
                  "href": "http://localhost:8080/api/reports/00002/accident"
                }
              }
            }
        ]
    },
    "page": {
        "size": 20,
        "totalElements": 4,
        "totalPages": 1,
        "number": 0
    }
}

If the _embedded is Array and city in report[0] and report[1] is the same, then city in report[1] will not be fetched. That's why we should have linkMap in each _embeddedItems.

Any solution for this issue ?

from angular-spring-data-rest.

guylabs avatar guylabs commented on May 28, 2024

Hi Romy,

currently not as I do not have the time for it. But maybe you could come up with a solution as you have the context and currently work with it more than I do ;). Then we can have a look at it and merge it into the master.

Thanks and regards,

Guy

from angular-spring-data-rest.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.