angular-restmod
angular-restmod copied to clipboard
hasOne returning an array
The backend I'm working with has some endpoints that are modeled in a way such that I'm finding difficult to model with restmod.
As an example: /things/:thingId/options/me
In the above example, I might want to model it like this:
restmod.model('things').mix({
options: { hasMany: restmod.model().mix({
me: { hasOne: restmod.model() }
})}
});
That doesn't quite work, because me
returns an array of options instead of a single object.
I am wondering if this works:
restmod.model('things').mix({
myOptions: { hasMany: restmod.model(), path: 'options/me' }
});
It seems like I should be able to fetch them with thing.$new(id).myOptions.$fetch().
Does that seem like the best way to go? Do you have any other insights (other than changing the URL to /things/:thingId/options?mine=true)?
I think your second approach it's fine, can't think on another way.
Maybe this could better be solved by supporting collection suffix and collection scopes, so you could do something like:
restmod.model().mix({
options: { hasMany: 'Option', scopes: {
me: { path: 'me' },
others: { params: { other: true } }
}
});
I'm currently working in a hasMany
overhaul, I'll take this into consideration.