Skip to content

Conversation

@therealcljohn
Copy link
Contributor

This is a proposal to implement #26. I followed the idea to use generics (mentioned in the issue) and did test:

If there are more changes needed, please let me know :)

Clemens John added 3 commits October 20, 2016 15:24
…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>
Copy link
Owner

@ghidoz ghidoz left a 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.


export class DocumentModel<T> {
_links: LinksModel = new LinksModel;
_data: T;
Copy link
Owner

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

return this._links.links(name);
}

public data(): T {
Copy link
Owner

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 {...}

return this._data;
}

public setData(data: any) {
Copy link
Owner

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) {...}


id: string;
_links: LinksModel = new LinksModel;
_document: DocumentModel<JsonApiModel>;
Copy link
Owner

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

[key: string]: any;

constructor(private _datastore: JsonApiDatastore, data?: any) {
constructor(private _datastore: JsonApiDatastore, data?: any, document: DocumentModel<JsonApiModel> = null) {
Copy link
Owner

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 ;)

Copy link
Owner

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>

return peek;
}
let newObject: T = new modelType(this._datastore, data);
let newObject: T = new modelType(this._datastore, data, null);
Copy link
Owner

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

this._name = name;
}

name() {
Copy link
Owner

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

return this._name;
}

href() {
Copy link
Owner

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

});
}

public links(name: string = null) {
Copy link
Owner

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);
Copy link
Owner

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

@ghidoz
Copy link
Owner

ghidoz commented Oct 20, 2016

Moreover, you should also fix the few tests I did, since now are failing of course. Thanks man!

@HennerM
Copy link
Collaborator

HennerM commented Oct 21, 2016

I for myself think that the JsonApiModel should not be extended, it doesn't really makes sense to include this property in every instance of JsonApiModel. Like @reva2 stated in #34 a JsonApiModel should just be a POJO.

@therealcljohn
Copy link
Contributor Author

Hi @ghidoz, thanks for your comments! I'll look at them next week when I also have time to look at the tests.

For the point that @HennerM made, I'm too much a newby in the Javascript world to discuss this topic so I leave this open to you.

@HennerM
Copy link
Collaborator

HennerM commented Oct 22, 2016

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.

@therealcljohn
Copy link
Contributor Author

@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).

@HennerM
Copy link
Collaborator

HennerM commented Oct 23, 2016

@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

Clemens John added 6 commits October 24, 2016 18:27
… 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>
@rnikunen
Copy link

rnikunen commented Feb 6, 2017

Is this going to be added to master anytime soon? I'm in need of accessing meta data!

@therealcljohn
Copy link
Contributor Author

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.

@rnikunen
Copy link

rnikunen commented Feb 7, 2017

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.

@rnikunen
Copy link

Just checkin in. We really need access to metadata for pagination and links.

@gabskoro
Copy link

Same here :)

@RooTooZ
Copy link
Contributor

RooTooZ commented Mar 9, 2017

@HennerM i'll create new merge request
Now we can make custom metadata
See in #64

PS
@gabskoro Can you see my merge?

@HennerM
Copy link
Collaborator

HennerM commented Jul 5, 2017

Closing in favor of #64

@HennerM HennerM closed this Jul 5, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants