Advertisement
Guest User

Observables Ionic 2

a guest
Aug 24th, 2016
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Ionic 2 && Angular RC4
  2.  
  3. //app.ts
  4. import { Component }      from '@angular/core';
  5. import { HTTP_PROVIDERS } from '@angular/http';
  6.  
  7. import { Platform, ionicBootstrap } from 'ionic-angular';
  8. import { StatusBar }      from 'ionic-native';
  9.  
  10. import { TabsPage }       from './pages/tabs/tabs';
  11. import { LoginPage }      from './pages/login/login';
  12.  
  13. import { DataService }    from './dashboard/data.service'
  14.  
  15.  
  16. @Component({
  17.   template: '<ion-nav [root]="rootPage"></ion-nav>',
  18.   providers: [DataService]
  19. })
  20.  
  21. export class MyApp {
  22.   private rootPage:any;
  23.  
  24.   constructor(private platform: Platform) {
  25.     var cache_settings = localStorage.getItem('cache_settings');
  26.     if(cache_settings === null){
  27.       localStorage.clear();
  28.       this.rootPage   = LoginPage;
  29.     } else {
  30.       this.rootPage   = TabsPage;
  31.     }
  32.  
  33.     platform.ready().then(() => {
  34.       // Okay, so the platform is ready and our plugins are available.
  35.       // Here you can do any higher level native things you might need.
  36.       StatusBar.styleDefault();
  37.     });
  38.   }
  39. }
  40.  
  41. ionicBootstrap(MyApp, [DataService])
  42.  
  43.  
  44.  
  45. //data.service.ts
  46. import { Injectable }                   from '@angular/core';
  47. import { Http }                         from '@angular/http';
  48.  
  49. import { Observable }                   from 'rxjs/Observable';
  50. import 'rxjs/rx';
  51.  
  52.  
  53. //import { App }                        from './../globals';
  54.  
  55.  
  56. @Injectable()
  57. export class DataService {
  58.     public data;
  59.     public charts        = {};
  60.     public instances:any = {};
  61.  
  62.     constructor(public http: Http){}
  63.  
  64.     fetchData(localData, project_id = null, callback?){
  65.         debugger;
  66.         //TODO: Look up ES6 Features I can use to bypasss this
  67.         window['App'].self = this;
  68.  
  69.         var cache = window.localStorage.getItem('cache_settings');
  70.             cache = (typeof cache !== 'undefined' && cache == 'true') ? true : false;
  71.  
  72.         if(localData !== null && typeof localData == 'string' && cache == true) {
  73.             console.log('fetching cached data:');
  74.             this.data = JSON.parse(localData);
  75.  
  76.         } else {
  77.             var observable = this.http.get('http://www.mysite.com/dash/projects/' + project_id).map( (data) => {
  78.                 return data.json();
  79.               }).subscribe(data => {
  80.                 console.log('Observable setting data: Project specific data came back with: ', data);
  81.  
  82.                 window.localStorage.setItem('project_data', JSON.stringify(data));
  83.                 window['App'].loading.dismiss();
  84.  
  85.                 this.data = data;
  86.                 if(typeof callback !== 'undefined') callback();
  87.             });
  88.         }
  89.  
  90.         return this.data;
  91.     }
  92.  
  93.     /* Cached project matches project being fetched */
  94.     isCurrentProject(projectId, newProjectId){
  95.         return (projectId === newProjectId);
  96.     }
  97.  
  98.     studiesDidChange(project_id, data = null){
  99.         var _data   = this.fetchData(window.localStorage.getItem('project_data'), project_id);
  100.         var data    = (data !== null) ? data : _data;
  101.  
  102.         return (project_id !== data.survey.id);
  103.     }
  104.  
  105.     shouldStoreData(data, project_id){
  106.         return (this.dataIsValid(data) && this.isCurrentProject(project_id, data.survey.id));
  107.     }
  108.  
  109.     /* Confirms data from local storage is an acutal response object */
  110.     dataIsValid(data){
  111.         return (typeof data == 'object' && data !== null);
  112.     }
  113.  
  114.     getData(){
  115.         return this.data;
  116.     }
  117.  
  118.     getProjectId(data = null){
  119.         return (this.dataIsValid(data)) ? data.id : window.localStorage.getItem('project_id');
  120.     }
  121.  
  122.     pushChart(chartType, data, chart){
  123.         var chart_id = chartType + '-' + data.survey.id;
  124.         this.charts[chart_id] = chart;
  125.     }
  126.  
  127.     removeCharts(){
  128.         for(var chartName in this.charts){
  129.             var chart = this.charts[chartName];
  130.             chart.detach();
  131.             chart.container.remove();
  132.         }
  133.     }
  134.  
  135.     reloadCharts(){
  136.         window['App'].self.instances.Dashboard.initializeDashboard();
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement