{"id":20931644,"url":"https://github.com/johnwargo/ionic-cascading-providers","last_synced_at":"2026-04-16T09:03:56.320Z","repository":{"id":70061672,"uuid":"75987889","full_name":"johnwargo/ionic-cascading-providers","owner":"johnwargo","description":"A simple prototype ","archived":false,"fork":false,"pushed_at":"2024-11-20T15:52:45.000Z","size":1039,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-01-19T19:10:02.160Z","etag":null,"topics":["ionic"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/johnwargo.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-12-09T00:52:56.000Z","updated_at":"2024-11-20T15:52:44.000Z","dependencies_parsed_at":"2023-02-21T22:00:56.488Z","dependency_job_id":null,"html_url":"https://github.com/johnwargo/ionic-cascading-providers","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/johnwargo%2Fionic-cascading-providers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/johnwargo%2Fionic-cascading-providers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/johnwargo%2Fionic-cascading-providers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/johnwargo%2Fionic-cascading-providers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/johnwargo","download_url":"https://codeload.github.com/johnwargo/ionic-cascading-providers/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243324505,"owners_count":20273113,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ionic"],"created_at":"2024-11-18T21:44:37.007Z","updated_at":"2025-12-25T09:47:55.675Z","avatar_url":"https://github.com/johnwargo.png","language":"JavaScript","funding_links":["https://www.buymeacoffee.com/johnwargo"],"categories":[],"sub_categories":[],"readme":"Ionic Cascading Providers\n=========================\n\nI am working on an Ionic 2 project, and for this project, the application needs to support a variety of data providers: online, offline, localstorage,  orsecurestorage. Basically, through a configuration setting or a hard-coded property in the application, the application needs to dynamically switch in the right provider based on the configuration. To prove one way to implement this, I created this sample project. It exposes a single Ionic provider that abstracts away two other providers. \n\nthe `data.ts` provider sets a default provider in the constructor, then exposes a method called `setProvider` that enables another component to switch the provider.\n\n\timport { Injectable } from '@angular/core';\n\timport { Data1 } from './data-1';\n\timport { Data2 } from './data-2';\n\t\n\t@Injectable()\n\texport class Data {\n\t\n\t  public provider: any;\n\t\n\t  constructor(\n\t    private data1: Data1,\n\t    private data2: Data2\n\t  ) {\n\t    console.log('Data Provider: Initializing provider');\n\t    //Set the default provider\n\t    this.provider = this.data1;\n\t  }\n\t\n\t  public setProvider(provider: number) {\n\t    console.log('Data Provider: Entering setProvider()');\n\t    //Validate input\n\t    if (provider \u003c 3) {\n\t      //switch to the selected provider\n\t      switch (provider) {\n\t        case 1:\n\t          console.log('Data Provider: Setting provider Data1');\n\t          this.provider = this.data1;\n\t          break;\n\t        case 2:\n\t          console.log('Data Provider: Setting provider Data2');\n\t          this.provider = this.data2;\n\t          break;\n\t      }\n\t    } else {\n\t      console.warn('Data Provider: Invalid provider number');\n\t    }\n\t  }\n\t\n\t}\n\nProviders `data1.ts` and `data2.ts` both expose a single method: `test()` that simply writes some text to the console indicating which provider created the output.\n\n\timport { Injectable } from '@angular/core';\n\t\n\t@Injectable()\n\texport class Data1 {\n\t\n\t  constructor() {\n\t    console.log('Initializing Data-1')\n\t  }\n\t\n\t  test() {\n\t    console.log('Output from Data-1');\n\t  }\n\t}\n\nWhen you run the application, it exposes several buttons. Click the **Output Text** button to call the `test()` method on the selected provider. Tap the **Data Provider #1** and **Data Provider #2** to switch output to the selected provider.\n\n![Screenshot](screenshots/figure-01.png)\n\n***\n\nYou can find information on many different topics on my [personal blog](http://www.johnwargo.com). Learn about all of my publications at [John Wargo Books](http://www.johnwargobooks.com).\n\nIf you find this code useful and feel like thanking me for providing it, please consider \u003ca href=\"https://www.buymeacoffee.com/johnwargo\" target=\"_blank\"\u003eBuying Me a Coffee\u003c/a\u003e, or making a purchase from [my Amazon Wish List](https://amzn.com/w/1WI6AAUKPT5P9).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjohnwargo%2Fionic-cascading-providers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjohnwargo%2Fionic-cascading-providers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjohnwargo%2Fionic-cascading-providers/lists"}