angularfire icon indicating copy to clipboard operation
angularfire copied to clipboard

Cast Query Observable to FirebaseListObservable

Open davideast opened this issue 10 years ago • 6 comments

If you create a query on a FirebaseListObservable the methods such as .push(), .remove(), and .update() are never attached.


// a non-queried list
cost regularList = af.database.list(`/items`);

// a queried list
const queriedList = af.database.list(`/items`, {
  query: { limitToLast: 25 }
});

// works
regularList.push({ name 'item' });

// won't work
queriedList.push({ name: 'item' });

I believe this is because a query creates a regular Observable, and then a mergeMap is used to create a FirebaseListObservable.

Any ideas on how to solve this issue, @blesh?

davideast avatar May 10 '16 11:05 davideast

@davideast you'd need to create a custom Observable that overrides lift to return it's own type. Something like:

class FirebaseListObservable<T> extends Observable<T> {
   /* stuff here */
  lift<R>(operator: Operator<T, R>): FirebaseListObservable<R> {
    const observable = new FirebaseListObservable<R>();
    observable.source = this;
    observable.operator = operator;
    return observable;
  }
}

This is because lift is used in every operator to create the operation/effect. Take map for example

benlesh avatar May 11 '16 18:05 benlesh

Another thing to do is question whether or not is that you want a custom Observable type, or you want a class that has an observable as a property on it. It really depends on the ergonomics you're aiming for.

benlesh avatar May 11 '16 18:05 benlesh

Is there a work around for this issue?

ian-a-hickey avatar Jun 18 '16 20:06 ian-a-hickey

Thanks for the update @davideast.

ian-a-hickey avatar Jun 21 '16 23:06 ian-a-hickey

Hey David, first congratulations for building this awesome library!! I just started to use it and is great! I see that this issue has been closed. This means it was fixed? If so, how come i still have the same problem when using push? I'm using "angularfire2": "^2.0.0-beta.3" and "firebase": "^3.2.1"

Or this problem is because i'm using angular rc4 and not rc3 ?

If this is not fixed yet and will not be fixed quickly - it's a good ideea to use 2 different observables linked to the same database source? I know it's repetitive but seems to work. For example:

this.getMessages = this.fb.database.list('messages', { // query here}); // used only for rendering + dynamic queering

this.saveMessages = this.fb.database.list('/messages')  // no query,
this,saveMessages.push(data) // this works now - i can save the data.

Other than being repetitive is there a problem using this pattern until this issue is rely fixed? Thank's so much for your time:)

AionDev avatar Jul 31 '16 14:07 AionDev

@AIon-Gbbe does this break | async for you?

matt-erhart avatar Aug 03 '16 00:08 matt-erhart