List component using RXJS Angular
up vote
2
down vote
favorite
I have a small angular project, where I have an Article component to list articles with pagination system.
The default page
The placeholder loader page
The main objectives are:
- Display the articles after consuming a GET API ressource.
- Paginating the articles with ngb-pagination.
- Display a placeholder loader in case the response is not ready yet.
My code works perfectly, except that I notice a little refresh of the page each time I paginate to another page. I'm sure that my code need a lot of improvements.
I'm gonna show you my code, and I ask you to help me to improve it. (you can find here on GitHub.
This my article.compononent.ts
export class ArticleComponent implements OnInit {
itemsPerPage: any;
totalItems: any;
page: any;
previousPage: any;
public observable$: Observable<any>;
private _articlesUrl = `${environment.apiUrl}` + '/articles';
public articles: any;
readonly imagePath = `${environment.apiUrl}` + '/..';
constructor(
private http: HttpClient,
) { }
ngOnInit() {
this.observable$ = this.http.get(this._articlesUrl).pipe(map((res: Response) => {
this.page = res['page'];
this.totalItems = res['total'];
this.itemsPerPage = res['limit'];
return res['_embedded']['items'];
}));
this.observable$.subscribe(
(res) => this.articles = res
);
}
loadPage(page: number) {
if (page !== this.previousPage) {
this.previousPage = page;
this.loadData();
}
}
loadData() {
this.http.get(this._articlesUrl, {
params: new HttpParams()
.set('page', this.page)
.set('per_page', this.itemsPerPage)
}).pipe(map((res) => res['_embedded']['items'])).subscribe(
(res: any) => this.articles = res
);
}
trackElement(index: number, element: any) {
return element ? element.id : null;
}
createRange(len= 20) {
const arr = ;
for (let i = 0; i < len ; i++) {
arr.push(i);
}
return arr;
}
}
This is the article.component.html
<div class="col-xs-12" *ngIf="articles">
<div class="blog-grids">
<div class="grid" *ngFor="let article of (articles); trackBy: trackElement">
<div class="entry-media" >
<img [src]="imagePath + article.image" alt="">
</div>
<div class="entry-body">
<span class="cat">{{article.title}}</span>
<h3><a [routerLink]="['/article/', article.id]">Visit {{article.title}}</a></h3>
<p>Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut..</p>
<div class="read-more-date">
<a href="#">Read More..</a>
<span class="date">3 Hours ago</span>
</div>
</div>
</div>
</div>
<ngb-pagination [collectionSize]="totalItems" [pageSize]="itemsPerPage" [(page)]="page" [maxSize]="2" [rotate]="true" (pageChange)="loadPage($event)"></ngb-pagination>
</div>
<masonry class="row mt-5" *ngIf="!(observable$ | async)">
<masonry-brick class="col-md-4 mb-3" *ngFor="let item of createRange(6)">
<div class="timeline-item">
<div class="animated-background">
<div class="background-masker image"></div>
<div class="background-masker header-bottom"></div>
...
</div>
</div>
</masonry-brick>
</masonry>
Any code review, improvement, refactoring, recommendation, advices, pull-request would be appreciated and voted.
EDIT
This is what my api return as a result:
{
"_embedded": {
"items": [
{
"categoryLabel": "category_label",
"id": 275,
"title": "Miss",
"image": "/images/articles/9f9d5128ba85f715d5bf0c72d9609b89.jpg"
},
{
"categoryLabel": "category_label",
"id": 276,
"title": "Dr.",
"image": "/images/articles/79f58611ecfe946bc41a5aba403c2c3c.jpg"
},
{
"categoryLabel": "category_label",
"id": 277,
"title": "Mr.",
"image": "/images/articles/920574ee7bcf374e2b68eb7d698a64a4.jpg"
},
{
"categoryLabel": "category_label",
"id": 278,
"title": "Prof.",
"image": "/images/articles/b5d6da63fa2db61ac4e4e8ca01568a0d.jpg"
},
{
"categoryLabel": "category_label",
"id": 279,
"title": "Prof.",
"image": "/images/articles/940d7dcbd80f8ff8f3fd707bd92f681d.jpg"
}
]
},
"route": "api_get_articles",
"parameters": ,
"absolute": false,
"limit": 5,
"total": 250,
"limitparametername": "per_page",
"page": 1,
"pages": 50,
"pageparametername": "page",
}
}
typescript pagination angular-2+ rxjs
add a comment |
up vote
2
down vote
favorite
I have a small angular project, where I have an Article component to list articles with pagination system.
The default page
The placeholder loader page
The main objectives are:
- Display the articles after consuming a GET API ressource.
- Paginating the articles with ngb-pagination.
- Display a placeholder loader in case the response is not ready yet.
My code works perfectly, except that I notice a little refresh of the page each time I paginate to another page. I'm sure that my code need a lot of improvements.
I'm gonna show you my code, and I ask you to help me to improve it. (you can find here on GitHub.
This my article.compononent.ts
export class ArticleComponent implements OnInit {
itemsPerPage: any;
totalItems: any;
page: any;
previousPage: any;
public observable$: Observable<any>;
private _articlesUrl = `${environment.apiUrl}` + '/articles';
public articles: any;
readonly imagePath = `${environment.apiUrl}` + '/..';
constructor(
private http: HttpClient,
) { }
ngOnInit() {
this.observable$ = this.http.get(this._articlesUrl).pipe(map((res: Response) => {
this.page = res['page'];
this.totalItems = res['total'];
this.itemsPerPage = res['limit'];
return res['_embedded']['items'];
}));
this.observable$.subscribe(
(res) => this.articles = res
);
}
loadPage(page: number) {
if (page !== this.previousPage) {
this.previousPage = page;
this.loadData();
}
}
loadData() {
this.http.get(this._articlesUrl, {
params: new HttpParams()
.set('page', this.page)
.set('per_page', this.itemsPerPage)
}).pipe(map((res) => res['_embedded']['items'])).subscribe(
(res: any) => this.articles = res
);
}
trackElement(index: number, element: any) {
return element ? element.id : null;
}
createRange(len= 20) {
const arr = ;
for (let i = 0; i < len ; i++) {
arr.push(i);
}
return arr;
}
}
This is the article.component.html
<div class="col-xs-12" *ngIf="articles">
<div class="blog-grids">
<div class="grid" *ngFor="let article of (articles); trackBy: trackElement">
<div class="entry-media" >
<img [src]="imagePath + article.image" alt="">
</div>
<div class="entry-body">
<span class="cat">{{article.title}}</span>
<h3><a [routerLink]="['/article/', article.id]">Visit {{article.title}}</a></h3>
<p>Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut..</p>
<div class="read-more-date">
<a href="#">Read More..</a>
<span class="date">3 Hours ago</span>
</div>
</div>
</div>
</div>
<ngb-pagination [collectionSize]="totalItems" [pageSize]="itemsPerPage" [(page)]="page" [maxSize]="2" [rotate]="true" (pageChange)="loadPage($event)"></ngb-pagination>
</div>
<masonry class="row mt-5" *ngIf="!(observable$ | async)">
<masonry-brick class="col-md-4 mb-3" *ngFor="let item of createRange(6)">
<div class="timeline-item">
<div class="animated-background">
<div class="background-masker image"></div>
<div class="background-masker header-bottom"></div>
...
</div>
</div>
</masonry-brick>
</masonry>
Any code review, improvement, refactoring, recommendation, advices, pull-request would be appreciated and voted.
EDIT
This is what my api return as a result:
{
"_embedded": {
"items": [
{
"categoryLabel": "category_label",
"id": 275,
"title": "Miss",
"image": "/images/articles/9f9d5128ba85f715d5bf0c72d9609b89.jpg"
},
{
"categoryLabel": "category_label",
"id": 276,
"title": "Dr.",
"image": "/images/articles/79f58611ecfe946bc41a5aba403c2c3c.jpg"
},
{
"categoryLabel": "category_label",
"id": 277,
"title": "Mr.",
"image": "/images/articles/920574ee7bcf374e2b68eb7d698a64a4.jpg"
},
{
"categoryLabel": "category_label",
"id": 278,
"title": "Prof.",
"image": "/images/articles/b5d6da63fa2db61ac4e4e8ca01568a0d.jpg"
},
{
"categoryLabel": "category_label",
"id": 279,
"title": "Prof.",
"image": "/images/articles/940d7dcbd80f8ff8f3fd707bd92f681d.jpg"
}
]
},
"route": "api_get_articles",
"parameters": ,
"absolute": false,
"limit": 5,
"total": 250,
"limitparametername": "per_page",
"page": 1,
"pages": 50,
"pageparametername": "page",
}
}
typescript pagination angular-2+ rxjs
1
Welcome to Code Review! After getting an answer you should not change your code anymore. This is to ensure that answers do not get invalidated and have to hit a moving target. If you have changed your code you can either post it as an answer (if it would constitute a code review) or ask a new question with your changed code (linking back to this one as reference). Refer to this post for more information
– Sᴀᴍ Onᴇᴌᴀ
Dec 12 at 18:09
1
Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers. I've rolled back to revision 5.
– Mast
Dec 12 at 18:30
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have a small angular project, where I have an Article component to list articles with pagination system.
The default page
The placeholder loader page
The main objectives are:
- Display the articles after consuming a GET API ressource.
- Paginating the articles with ngb-pagination.
- Display a placeholder loader in case the response is not ready yet.
My code works perfectly, except that I notice a little refresh of the page each time I paginate to another page. I'm sure that my code need a lot of improvements.
I'm gonna show you my code, and I ask you to help me to improve it. (you can find here on GitHub.
This my article.compononent.ts
export class ArticleComponent implements OnInit {
itemsPerPage: any;
totalItems: any;
page: any;
previousPage: any;
public observable$: Observable<any>;
private _articlesUrl = `${environment.apiUrl}` + '/articles';
public articles: any;
readonly imagePath = `${environment.apiUrl}` + '/..';
constructor(
private http: HttpClient,
) { }
ngOnInit() {
this.observable$ = this.http.get(this._articlesUrl).pipe(map((res: Response) => {
this.page = res['page'];
this.totalItems = res['total'];
this.itemsPerPage = res['limit'];
return res['_embedded']['items'];
}));
this.observable$.subscribe(
(res) => this.articles = res
);
}
loadPage(page: number) {
if (page !== this.previousPage) {
this.previousPage = page;
this.loadData();
}
}
loadData() {
this.http.get(this._articlesUrl, {
params: new HttpParams()
.set('page', this.page)
.set('per_page', this.itemsPerPage)
}).pipe(map((res) => res['_embedded']['items'])).subscribe(
(res: any) => this.articles = res
);
}
trackElement(index: number, element: any) {
return element ? element.id : null;
}
createRange(len= 20) {
const arr = ;
for (let i = 0; i < len ; i++) {
arr.push(i);
}
return arr;
}
}
This is the article.component.html
<div class="col-xs-12" *ngIf="articles">
<div class="blog-grids">
<div class="grid" *ngFor="let article of (articles); trackBy: trackElement">
<div class="entry-media" >
<img [src]="imagePath + article.image" alt="">
</div>
<div class="entry-body">
<span class="cat">{{article.title}}</span>
<h3><a [routerLink]="['/article/', article.id]">Visit {{article.title}}</a></h3>
<p>Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut..</p>
<div class="read-more-date">
<a href="#">Read More..</a>
<span class="date">3 Hours ago</span>
</div>
</div>
</div>
</div>
<ngb-pagination [collectionSize]="totalItems" [pageSize]="itemsPerPage" [(page)]="page" [maxSize]="2" [rotate]="true" (pageChange)="loadPage($event)"></ngb-pagination>
</div>
<masonry class="row mt-5" *ngIf="!(observable$ | async)">
<masonry-brick class="col-md-4 mb-3" *ngFor="let item of createRange(6)">
<div class="timeline-item">
<div class="animated-background">
<div class="background-masker image"></div>
<div class="background-masker header-bottom"></div>
...
</div>
</div>
</masonry-brick>
</masonry>
Any code review, improvement, refactoring, recommendation, advices, pull-request would be appreciated and voted.
EDIT
This is what my api return as a result:
{
"_embedded": {
"items": [
{
"categoryLabel": "category_label",
"id": 275,
"title": "Miss",
"image": "/images/articles/9f9d5128ba85f715d5bf0c72d9609b89.jpg"
},
{
"categoryLabel": "category_label",
"id": 276,
"title": "Dr.",
"image": "/images/articles/79f58611ecfe946bc41a5aba403c2c3c.jpg"
},
{
"categoryLabel": "category_label",
"id": 277,
"title": "Mr.",
"image": "/images/articles/920574ee7bcf374e2b68eb7d698a64a4.jpg"
},
{
"categoryLabel": "category_label",
"id": 278,
"title": "Prof.",
"image": "/images/articles/b5d6da63fa2db61ac4e4e8ca01568a0d.jpg"
},
{
"categoryLabel": "category_label",
"id": 279,
"title": "Prof.",
"image": "/images/articles/940d7dcbd80f8ff8f3fd707bd92f681d.jpg"
}
]
},
"route": "api_get_articles",
"parameters": ,
"absolute": false,
"limit": 5,
"total": 250,
"limitparametername": "per_page",
"page": 1,
"pages": 50,
"pageparametername": "page",
}
}
typescript pagination angular-2+ rxjs
I have a small angular project, where I have an Article component to list articles with pagination system.
The default page
The placeholder loader page
The main objectives are:
- Display the articles after consuming a GET API ressource.
- Paginating the articles with ngb-pagination.
- Display a placeholder loader in case the response is not ready yet.
My code works perfectly, except that I notice a little refresh of the page each time I paginate to another page. I'm sure that my code need a lot of improvements.
I'm gonna show you my code, and I ask you to help me to improve it. (you can find here on GitHub.
This my article.compononent.ts
export class ArticleComponent implements OnInit {
itemsPerPage: any;
totalItems: any;
page: any;
previousPage: any;
public observable$: Observable<any>;
private _articlesUrl = `${environment.apiUrl}` + '/articles';
public articles: any;
readonly imagePath = `${environment.apiUrl}` + '/..';
constructor(
private http: HttpClient,
) { }
ngOnInit() {
this.observable$ = this.http.get(this._articlesUrl).pipe(map((res: Response) => {
this.page = res['page'];
this.totalItems = res['total'];
this.itemsPerPage = res['limit'];
return res['_embedded']['items'];
}));
this.observable$.subscribe(
(res) => this.articles = res
);
}
loadPage(page: number) {
if (page !== this.previousPage) {
this.previousPage = page;
this.loadData();
}
}
loadData() {
this.http.get(this._articlesUrl, {
params: new HttpParams()
.set('page', this.page)
.set('per_page', this.itemsPerPage)
}).pipe(map((res) => res['_embedded']['items'])).subscribe(
(res: any) => this.articles = res
);
}
trackElement(index: number, element: any) {
return element ? element.id : null;
}
createRange(len= 20) {
const arr = ;
for (let i = 0; i < len ; i++) {
arr.push(i);
}
return arr;
}
}
This is the article.component.html
<div class="col-xs-12" *ngIf="articles">
<div class="blog-grids">
<div class="grid" *ngFor="let article of (articles); trackBy: trackElement">
<div class="entry-media" >
<img [src]="imagePath + article.image" alt="">
</div>
<div class="entry-body">
<span class="cat">{{article.title}}</span>
<h3><a [routerLink]="['/article/', article.id]">Visit {{article.title}}</a></h3>
<p>Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut..</p>
<div class="read-more-date">
<a href="#">Read More..</a>
<span class="date">3 Hours ago</span>
</div>
</div>
</div>
</div>
<ngb-pagination [collectionSize]="totalItems" [pageSize]="itemsPerPage" [(page)]="page" [maxSize]="2" [rotate]="true" (pageChange)="loadPage($event)"></ngb-pagination>
</div>
<masonry class="row mt-5" *ngIf="!(observable$ | async)">
<masonry-brick class="col-md-4 mb-3" *ngFor="let item of createRange(6)">
<div class="timeline-item">
<div class="animated-background">
<div class="background-masker image"></div>
<div class="background-masker header-bottom"></div>
...
</div>
</div>
</masonry-brick>
</masonry>
Any code review, improvement, refactoring, recommendation, advices, pull-request would be appreciated and voted.
EDIT
This is what my api return as a result:
{
"_embedded": {
"items": [
{
"categoryLabel": "category_label",
"id": 275,
"title": "Miss",
"image": "/images/articles/9f9d5128ba85f715d5bf0c72d9609b89.jpg"
},
{
"categoryLabel": "category_label",
"id": 276,
"title": "Dr.",
"image": "/images/articles/79f58611ecfe946bc41a5aba403c2c3c.jpg"
},
{
"categoryLabel": "category_label",
"id": 277,
"title": "Mr.",
"image": "/images/articles/920574ee7bcf374e2b68eb7d698a64a4.jpg"
},
{
"categoryLabel": "category_label",
"id": 278,
"title": "Prof.",
"image": "/images/articles/b5d6da63fa2db61ac4e4e8ca01568a0d.jpg"
},
{
"categoryLabel": "category_label",
"id": 279,
"title": "Prof.",
"image": "/images/articles/940d7dcbd80f8ff8f3fd707bd92f681d.jpg"
}
]
},
"route": "api_get_articles",
"parameters": ,
"absolute": false,
"limit": 5,
"total": 250,
"limitparametername": "per_page",
"page": 1,
"pages": 50,
"pageparametername": "page",
}
}
typescript pagination angular-2+ rxjs
typescript pagination angular-2+ rxjs
edited Dec 12 at 18:28
Mast
7,43963686
7,43963686
asked Dec 11 at 20:16
ahmedbhs
1114
1114
1
Welcome to Code Review! After getting an answer you should not change your code anymore. This is to ensure that answers do not get invalidated and have to hit a moving target. If you have changed your code you can either post it as an answer (if it would constitute a code review) or ask a new question with your changed code (linking back to this one as reference). Refer to this post for more information
– Sᴀᴍ Onᴇᴌᴀ
Dec 12 at 18:09
1
Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers. I've rolled back to revision 5.
– Mast
Dec 12 at 18:30
add a comment |
1
Welcome to Code Review! After getting an answer you should not change your code anymore. This is to ensure that answers do not get invalidated and have to hit a moving target. If you have changed your code you can either post it as an answer (if it would constitute a code review) or ask a new question with your changed code (linking back to this one as reference). Refer to this post for more information
– Sᴀᴍ Onᴇᴌᴀ
Dec 12 at 18:09
1
Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers. I've rolled back to revision 5.
– Mast
Dec 12 at 18:30
1
1
Welcome to Code Review! After getting an answer you should not change your code anymore. This is to ensure that answers do not get invalidated and have to hit a moving target. If you have changed your code you can either post it as an answer (if it would constitute a code review) or ask a new question with your changed code (linking back to this one as reference). Refer to this post for more information
– Sᴀᴍ Onᴇᴌᴀ
Dec 12 at 18:09
Welcome to Code Review! After getting an answer you should not change your code anymore. This is to ensure that answers do not get invalidated and have to hit a moving target. If you have changed your code you can either post it as an answer (if it would constitute a code review) or ask a new question with your changed code (linking back to this one as reference). Refer to this post for more information
– Sᴀᴍ Onᴇᴌᴀ
Dec 12 at 18:09
1
1
Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers. I've rolled back to revision 5.
– Mast
Dec 12 at 18:30
Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers. I've rolled back to revision 5.
– Mast
Dec 12 at 18:30
add a comment |
1 Answer
1
active
oldest
votes
up vote
3
down vote
The purpose of the request made in ngOnInit
seemed unclear to me.
I presume if any pagination info is not provided, then the result contains the default pagination values such as page
, total
, limit
or does it return all items?
Just in case, it is good practice to provide explicit default values such as page: 0
, limit: 20
etc. After setting the default values, ngOnInit
should use loadData
to fetch the items.
As far as i see, observable$
variable is not used anywhere except ngOnInit
, so i would remove it.
For fetching data or another possible operations on item(s) such as update, fetch, delete etc, i would create a ItemService
. You can prefer it to be inject to your component.
Since typescript is a typed language i would define types for response and item.
Such as
interface Item {
}
interface ItemResponse {
page: number;
total: number;
limit: number;
items: Item;
}
So from ItemService
you may return observable of the proper type.
Edit: updated comment about observable$
variable.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209470%2flist-component-using-rxjs-angular%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
The purpose of the request made in ngOnInit
seemed unclear to me.
I presume if any pagination info is not provided, then the result contains the default pagination values such as page
, total
, limit
or does it return all items?
Just in case, it is good practice to provide explicit default values such as page: 0
, limit: 20
etc. After setting the default values, ngOnInit
should use loadData
to fetch the items.
As far as i see, observable$
variable is not used anywhere except ngOnInit
, so i would remove it.
For fetching data or another possible operations on item(s) such as update, fetch, delete etc, i would create a ItemService
. You can prefer it to be inject to your component.
Since typescript is a typed language i would define types for response and item.
Such as
interface Item {
}
interface ItemResponse {
page: number;
total: number;
limit: number;
items: Item;
}
So from ItemService
you may return observable of the proper type.
Edit: updated comment about observable$
variable.
add a comment |
up vote
3
down vote
The purpose of the request made in ngOnInit
seemed unclear to me.
I presume if any pagination info is not provided, then the result contains the default pagination values such as page
, total
, limit
or does it return all items?
Just in case, it is good practice to provide explicit default values such as page: 0
, limit: 20
etc. After setting the default values, ngOnInit
should use loadData
to fetch the items.
As far as i see, observable$
variable is not used anywhere except ngOnInit
, so i would remove it.
For fetching data or another possible operations on item(s) such as update, fetch, delete etc, i would create a ItemService
. You can prefer it to be inject to your component.
Since typescript is a typed language i would define types for response and item.
Such as
interface Item {
}
interface ItemResponse {
page: number;
total: number;
limit: number;
items: Item;
}
So from ItemService
you may return observable of the proper type.
Edit: updated comment about observable$
variable.
add a comment |
up vote
3
down vote
up vote
3
down vote
The purpose of the request made in ngOnInit
seemed unclear to me.
I presume if any pagination info is not provided, then the result contains the default pagination values such as page
, total
, limit
or does it return all items?
Just in case, it is good practice to provide explicit default values such as page: 0
, limit: 20
etc. After setting the default values, ngOnInit
should use loadData
to fetch the items.
As far as i see, observable$
variable is not used anywhere except ngOnInit
, so i would remove it.
For fetching data or another possible operations on item(s) such as update, fetch, delete etc, i would create a ItemService
. You can prefer it to be inject to your component.
Since typescript is a typed language i would define types for response and item.
Such as
interface Item {
}
interface ItemResponse {
page: number;
total: number;
limit: number;
items: Item;
}
So from ItemService
you may return observable of the proper type.
Edit: updated comment about observable$
variable.
The purpose of the request made in ngOnInit
seemed unclear to me.
I presume if any pagination info is not provided, then the result contains the default pagination values such as page
, total
, limit
or does it return all items?
Just in case, it is good practice to provide explicit default values such as page: 0
, limit: 20
etc. After setting the default values, ngOnInit
should use loadData
to fetch the items.
As far as i see, observable$
variable is not used anywhere except ngOnInit
, so i would remove it.
For fetching data or another possible operations on item(s) such as update, fetch, delete etc, i would create a ItemService
. You can prefer it to be inject to your component.
Since typescript is a typed language i would define types for response and item.
Such as
interface Item {
}
interface ItemResponse {
page: number;
total: number;
limit: number;
items: Item;
}
So from ItemService
you may return observable of the proper type.
Edit: updated comment about observable$
variable.
edited Dec 12 at 15:20
answered Dec 12 at 10:11
sardok
1413
1413
add a comment |
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209470%2flist-component-using-rxjs-angular%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Welcome to Code Review! After getting an answer you should not change your code anymore. This is to ensure that answers do not get invalidated and have to hit a moving target. If you have changed your code you can either post it as an answer (if it would constitute a code review) or ask a new question with your changed code (linking back to this one as reference). Refer to this post for more information
– Sᴀᴍ Onᴇᴌᴀ
Dec 12 at 18:09
1
Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers. I've rolled back to revision 5.
– Mast
Dec 12 at 18:30