Open sazzer opened 8 years ago
Unless I have missed something this is still an issue in ^5.0.1.
Does anyone know of a workaround?
Thanks
Hey there,
I need to go through and clean up these old tickets. The best way to implement toHal()
now is to make use of the entity's configure()
method. its simpler and it works just like configuring the route:
PageModel.prototype.toHal = function (rep, next) {
rep.configure({
embedded: {
item: {
path: 'items',
href: './{item.id}'
}
}
}, next);
};
Hi @bleupen,
Thanks a lot for the heads-up, that's really helped simplify a lot of my models toHal() methods and HAPI plugin config.
Cheers
Alex
Embedding an entity that defines a toHal method on it inside another method with a toHal method on it works perfectly well, iff the embedding is done by use of configuration. If it is done using code in the toHal method of the outer entity then it does not work as expected.
The following works as expected:
And this produces the following output:
However, if I try and do this with code instead, as follows:
And this produces the following output:
Note in this case the inner Item does not have a link of "owner" even though the actual InnerModel class is exactly the same in both cases. The only difference is the one embeds the items using the "embedded" configuration and the other does it using "rep.embed" in the toHal method.
Note that I wanted to do it this way so that I can have a single PageModel class that is used everywhere instead of repeating the configuration everywhere that I want to do this.