-
Notifications
You must be signed in to change notification settings - Fork 122
Support for more general json:api documents (links, data, meta) #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…nd meta attributes. Therefore refactor query and findRecord methods to return an instance of DocumentModel. Signed-off-by: Clemens John <clemens.john@floh1111.de>
Signed-off-by: Clemens John <clemens.john@floh1111.de>
Signed-off-by: Clemens John <clemens.john@floh1111.de>
ghidoz
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! I haven't tested it yet, but I just added some comments about style, so that it's more consistent with mine.
src/models/document.model.ts
Outdated
|
|
||
| export class DocumentModel<T> { | ||
| _links: LinksModel = new LinksModel; | ||
| _data: T; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe you can set these two as private
src/models/document.model.ts
Outdated
| return this._links.links(name); | ||
| } | ||
|
|
||
| public data(): T { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this can be a getter:
get data(): T {...}
src/models/document.model.ts
Outdated
| return this._data; | ||
| } | ||
|
|
||
| public setData(data: any) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this can be a setter:
set data(data: any) {...}
src/models/json-api.model.ts
Outdated
|
|
||
| id: string; | ||
| _links: LinksModel = new LinksModel; | ||
| _document: DocumentModel<JsonApiModel>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe you can set these two as private
src/models/json-api.model.ts
Outdated
| [key: string]: any; | ||
|
|
||
| constructor(private _datastore: JsonApiDatastore, data?: any) { | ||
| constructor(private _datastore: JsonApiDatastore, data?: any, document: DocumentModel<JsonApiModel> = null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you use the privatehere, you can remove the line 14 and it's cleaner and consistent ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moreover, instead of document: DocumentModel<JsonApiModel> = null you can write: document?: DocumentModel<JsonApiModel>
src/models/json-api.model.ts
Outdated
| return peek; | ||
| } | ||
| let newObject: T = new modelType(this._datastore, data); | ||
| let newObject: T = new modelType(this._datastore, data, null); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can just not passing a third argument and it's automatically null
src/models/link.model.ts
Outdated
| this._name = name; | ||
| } | ||
|
|
||
| name() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use a getter and a return type
src/models/link.model.ts
Outdated
| return this._name; | ||
| } | ||
|
|
||
| href() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use a getter and a return type
src/models/links.model.ts
Outdated
| }); | ||
| } | ||
|
|
||
| public links(name: string = null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use name?: string instead of name: string = null
|
|
||
| createRecord<T extends JsonApiModel>(modelType: ModelType<T>, data?: any): T { | ||
| return new modelType(this, {attributes: data}); | ||
| return new modelType(this, {attributes: data}, null); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to pass null
|
Moreover, you should also fix the few tests I did, since now are failing of course. Thanks man! |
|
I like the approach they took in this Java JSON-API library better, they allow defining meta and links attributes on model class level, for example: @Type("book")
public class Book {
@Id
private String isbn;
...
@Meta
private MyCustomMetaClass meta;
}This leaves the option to use meta and link attributes to the user. |
|
@HennerM As far as I understand this is for accessing these attributes if they are defined inside the data attribute arent they? But I need to access these attributes if they are defined at both levels: top level of the document and also on model level. For model level one can take the approach shown in the mentioned lib but to fully implementiert the spec we will need some Kind of DocumentModel returned by the request methods to access top level attributes (i.e. in case of an empty collection). |
|
@floh1111 sorry for the misunderstanding, of course I agree on the need for the DocumentModel class for accessing the top level metadata. But for model-level it would be better to define them with decorators in the model classes, to reduce the dependencies in JsonApiModel |
… and ModelType because I could not get it working with generics. The data property of DocumentModel can be T or T[] and I think ModelType needs more refactoring to return T and initialize DocumentModel<T[]> and DocumentModel<T> at the same time. Signed-off-by: Clemens John <clemens.john@floh1111.de>
Signed-off-by: Clemens John <clemens.john@floh1111.de>
Signed-off-by: Clemens John <clemens.john@floh1111.de>
… /home/floh1111/angular2-jsonapi/node_modules/@types/lodash/index.d.ts:245:25: Cannot find name 'Partial'. Signed-off-by: Clemens John <clemens.john@floh1111.de>
|
Is this going to be added to master anytime soon? I'm in need of accessing meta data! |
|
Hi @rnikunen I sadly had a project to finish in the past weeks but I have some more time now and I'm planning to take a look at this at the end of the week again. |
|
Thanks for the heads. I think it needs to be said....I appreciate that we all have jobs and these projects are a contribution to the community of angular devs. I have forked the project and am taking the approach @ghidoz mentioned back in October in the meantime. |
|
Just checkin in. We really need access to metadata for pagination and links. |
|
Same here :) |
|
Closing in favor of #64 |
This is a proposal to implement #26. I followed the idea to use generics (mentioned in the issue) and did test:
manually in my example project located at https://git.nordwest.freifunk.net/netmon-sc/web-client
If there are more changes needed, please let me know :)