Guest User

Untitled

a guest
Jun 24th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. import { Component } from '@angular/core';
  2. import { IonicPage, NavController, InfiniteScroll } from 'ionic-angular';
  3. import { HeroProvider } from '../../providers/hero/hero';
  4. import { Observable } from 'rxjs/Observable';
  5. import { CommonFunctionsProvider } from '../../providers/common-functions/common-functions'
  6. import { ViewChild } from '@angular/core';
  7. import { DescriptionPage } from '../description/description';
  8.  
  9. @Component({
  10. selector: 'page-home',
  11. templateUrl: 'home.html'
  12. })
  13. export class HomePage {
  14. public obj: any;
  15. public heroes: any;
  16. public limit: number = 5;
  17. public offset: number =5;
  18. public searchQuery: string = '';
  19. @ViewChild(InfiniteScroll) infiniteScroll: InfiniteScroll;
  20.  
  21. constructor(
  22. public navCtrl: NavController,
  23. public heroProvider: HeroProvider,
  24. public common: CommonFunctionsProvider
  25. ) {
  26. this.getAllHeroes({orderBy: 'name', limit: this.limit});
  27. }
  28.  
  29. getAllHeroes(queryString) {
  30. let usersObservable: Observable<[any]>;
  31. usersObservable = this.heroProvider.getHeroAll(this.common.encodeQueryData(queryString));
  32. this.obj =[];
  33. usersObservable.subscribe(
  34. data => {
  35. this.obj = data;
  36. if(this.heroes!= null)
  37. {
  38. this.obj.data.results.forEach(element => {
  39. this.heroes.push(element);
  40. });
  41. this.offset += 5;
  42. }
  43. else{
  44. this.heroes = this.obj.data.results;
  45. }
  46. if (this.infiniteScroll) {
  47. this.infiniteScroll.complete();
  48. if (this.heroes.length == this.obj.data.total) {
  49. this.infiniteScroll.enable(false);
  50. }
  51. }
  52. });
  53. }
  54.  
  55. searchHeroes(searchQuery) {
  56. console.log(this.common.encodeQueryData(searchQuery));
  57. let usersObservable: Observable<[any]>;
  58. usersObservable = this.heroProvider.getHeroAll(this.common.encodeQueryData(searchQuery));
  59. this.obj =[];
  60. this.heroes=[];
  61. usersObservable.subscribe(
  62. data => {
  63. this.obj = data;
  64. this.heroes = this.obj.data.results;
  65. }
  66. );
  67. }
  68.  
  69. getDescription(id){
  70. this.navCtrl.push('DescriptionPage', {
  71. id: id
  72. })
  73. }
  74.  
  75. onSearchChange(searchQuery) {
  76. if(searchQuery || searchQuery.trim() != '' ){
  77. setTimeout(() => {
  78. this.searchHeroes({orderBy: 'name', nameStartsWith: searchQuery});
  79. }, 200)
  80. }
  81. if(searchQuery || searchQuery.trim() == '' ){
  82. this.searchHeroes({orderBy: 'name', limit: this.limit});
  83. }
  84. }
  85.  
  86. }
Add Comment
Please, Sign In to add comment