Advertisement
Guest User

Untitled

a guest
Feb 26th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. import {Component, ViewChild, AfterViewInit} from '@angular/core';
  2. import {WpEndpoint, CollectionDirective, CollectionResponse} from '../../core/wordpress';
  3.  
  4. @Component({
  5. selector: 'feed-page',
  6. templateUrl: `
  7. <div class="feed" #feed [wpCollection]="postsEndpoint" [wpArgs]="postsArgs">
  8.  
  9. <ul>
  10. <li *ngFor="let post of posts">{{post.title()}}</li>
  11. </ul>
  12.  
  13. <button (click)="loadMore()">Load more</button>
  14. </div>
  15.  
  16. <loading-icon *ngIf="isLoading"></loading-icon>
  17. `
  18. })
  19. export class HomeComponent implements AfterViewInit {
  20.  
  21. /** Posts Endpoint */
  22. postsEndpoint = WpEndpoint.posts;
  23.  
  24. /** Posts Args */
  25. // in case you are using the filter plugin
  26. postsArgs = {
  27. 'filter[orderby]': 'rand',
  28. per_page: 10,
  29. _embed: true
  30. };
  31.  
  32. /** Posts Response */
  33. posts;
  34.  
  35. /** For scroll more and loading icon */
  36. postsLoading: boolean = false;
  37.  
  38. /** Get the directive reference */
  39. @ViewChild(CollectionDirective) feed: CollectionDirective;
  40.  
  41. ngAfterViewInit() {
  42. this.feed.response.subscribe((res) => {
  43. if (res) {
  44. if (res.error) {
  45. console.log('[Feed Posts]:', res.error);
  46. }
  47. if (res.data) {
  48. if (!this.bannerPosts) {
  49. /** Slice the first 3 posts for banner posts */
  50. this.bannerPosts = res.data.splice(0, 3);
  51. }
  52. this.feedPosts = res.data;
  53. }
  54. }
  55. });
  56. /** Show spinner loading */
  57. this.feed.wpLoading.distinctUntilChanged().subscribe((loading) => {
  58. this.postsLoading = loading;
  59. this.changeDetectorRef.markForCheck();
  60. });
  61. }
  62.  
  63.  
  64. /** Load more feed on scroll */
  65. loadMore() {
  66. this.feed.more();
  67. }
  68.  
  69. postResponse(res: CollectionResponse) {
  70. if (res) {
  71. if (res.error) {
  72. console.log('[Sidebar Posts]:', res.error);
  73. }
  74. if(!res.data.length){
  75.  
  76. }
  77. if (res.data) {
  78. this.sidebarPosts = res.data;
  79. }
  80. }
  81. }
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement