Quantcast
Channel: Ionic Forum - Latest topics
Viewing all articles
Browse latest Browse all 71531

Structuring an ionic 3 Firebase app

$
0
0

@obinnae wrote:

Can someone advice me on the best (or at least, a good) approach for an app that gets data from Firebase? My typescript/angular knowledge is a bit fuzzy, so bear with me.

Here’s my current scenario. An Ionic 3 app for buying/selling stuff. I have individual class files, such as …

export class Product
{
    id: number = new Date().getTime();
    name : string;
    // ... and so on
}

a service file (functions.ts) with all the functions (dozens of them) used on the app, one of the functions is like so …

 getProduct(id):Observable<Product>
 {
   return Observable.create(observer=>
   {
     console.log("id passed: ", id);
     firebase.database().ref('/products/'+id).once('value')
       .then((req)=>{observer.next(req.val())})
       .catch((err)=>{console.log(err)})
   });
 }
/// ... and lots more

then in the other pages, I do something like

import { myfunctions } from '../../providers/functions';
...
constructor(private myfuncs: myfunctions .... )
....
var prod;
this.myfuncs.getProduct(5).subscribe((p:Product)=>
{ prod = p;}

I’m not sure this is the best approach. I’m not too fund of the idea of importing the functions file in every page - I’d rather have each class contain its relevant functions (create(), edit(), delete(),…)

Ideally, I’d like to do something like this …

For the class (dunno if the syntax is correct. Please let me know)

export class Product
{
    id: number = new Date().getTime();
    name : string;
    // ... and so on

   constructor(id:number = null)
   {
    firebase.database().ref('products/' + id).once('value')
            .then((req)=>{return req.val();})
            .catch((err)=>{console.log(err)})
   }

  createProduct(newprod:Product)
  {  // add product to firebase  }

  deleteProduct(id:number)
  { // delete from firebase}

  // ...etc
}

then in other pages…


let prod = new Product(1) //returns the product with ID of 1

Is this a good approach, or can someone suggest a better one, or point me in the right direction? Thanks.

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 71531

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>