ardyhim

home.ts refresh

Sep 6th, 2016
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Component } from '@angular/core';
  2. import { NavController, ToastController } from 'ionic-angular';
  3. import { Api} from '../../providers/api/api';
  4. import { SinglePage } from '../single/single'
  5.  
  6. @Component({
  7.   templateUrl: 'build/pages/home/home.html',
  8.   providers: [Api]
  9. })
  10. export class HomePage {
  11.   datas:any;
  12.   type:boolean = false;
  13.   pagination: number = 1;
  14.   searchKeyword:any;
  15.   constructor(
  16.     public navCtrl: NavController,
  17.     private toast: ToastController,
  18.     private api: Api
  19.   ) {
  20.     api.index(1).subscribe( data => {
  21.       this.datas = data.json();
  22.     });
  23.   }
  24.  
  25.   openSinglePage(url){
  26.     this.navCtrl.push(SinglePage, {
  27.       url:url
  28.     })
  29.   }
  30.  
  31.   search(keyword){
  32.     this.type = true;
  33.     this.api.search(keyword,1).subscribe(data=>{
  34.       this.datas = data.json()
  35.     });
  36.   }
  37.  
  38.   onCencel(ev){
  39.     if(!ev.target.value){
  40.       this.type = false;
  41.       this.api.index(1).subscribe(data => {
  42.         this.datas = data.json();
  43.       })
  44.     }
  45.   }
  46.  
  47.   infiniteScroll(ev){
  48.     this.pagination++;
  49.     if(this.type === false){
  50.       this.api.index(this.pagination).subscribe(data=>{
  51.  
  52.         ev.complete()
  53.         if(data.json().length === 0){
  54.           let toast = this.toast.create({
  55.             message: 'No more posts',
  56.             duration: 3000
  57.           });
  58.           toast.present()
  59.         }else{
  60.           for(let i of data.json()){
  61.             this.datas.push(i);
  62.           }
  63.         }
  64.  
  65.       })
  66.     }else if(this.type === true){
  67.       this.api.search(this.searchKeyword, this.pagination).subscribe(data => {
  68.  
  69.         if(data.json().length === 0){
  70.           let toast = this.toast.create({
  71.             message: 'No more posts',
  72.             duration: 3000
  73.           });
  74.           toast.present();
  75.         }else{
  76.           for(let i of data.json()){
  77.             this.datas.push(i);
  78.           }
  79.         }
  80.  
  81.       });
  82.     }
  83.   }
  84.  
  85.   doRefresh(ev){
  86.     this.api.index(1).subscribe(data => {
  87.       ev.complete();
  88.       this.searchKeyword = "";
  89.       this.pagination = 1;
  90.       this.type = false;
  91.       this.datas = data.json();
  92.     });
  93.   }
  94.  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment