Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: $ref on Observables (make _ref public) #294

Closed
DennisSmolek opened this issue Jun 27, 2016 · 1 comment · Fixed by #447
Closed

Feature: $ref on Observables (make _ref public) #294

DennisSmolek opened this issue Jun 27, 2016 · 1 comment · Fixed by #447

Comments

@DennisSmolek
Copy link

Coming from Angular 1 and AngularFire I've been spoiled a bit with objects having direct access to the original ref and other helper functions. Before AF2 I wrote my own object but I like the Observable flow much better. Im having some issues though with relative paths and calling updates.

Consider a project and users that has the path like: /projects/<projectId>/teams/<teamId>/<userId>/favorite-colors

Ok, so first there are a lot of unknown keys that are needed to build the URL.
Let's say I want to add a favorite color to a user that's in a team. I have to get all the ID's and build the path relative to the root database.

constructor(private af: AngularFire) {
        this.teams = this.af.database.list('/projects/' + this.projectId);
    }

    addUserFieldValue(path: string,  value: string) {
        firebase().database().ref().child('projects/' + this.projectId + path).push(value);
    }

If we added the $ref you could simplify and call

addUserFieldValue(path:string, value: string) {
        this.teams.$ref.child(path).push(value);
    }

Otherwise you have to build the paths over and over when the ref is right there.

You could get it from the snapshot like this:

constructor(private af: AngularFire) {
        this.teams = this.af.database.list('/projects/' + this.projectId, { preserveSnapshot: true });
        this.teams.subscribe(snapshots => { 
            this.ref = snapshots.ref;
            this.teams = [];
            snapshots.forEach(snapshot => {
                var tempVal = snapshot.val();
                tempVal.$key = snapshot.key;
                this.teams.push(tempVal);
            });
         });
    }

    addUserFieldValue(path:string, value: string) {
        this.ref.child(path).push(value);
    }

But that seems WAY verbose to me..

The best part?

It's already there!
In the object there is _ref that is private... If we just change it to $ref and public it'll work..

I'm modding my version making _ref public, but figured it would be an easy change and then thought there might be a reason why it wasn't included so I figured I'd ask.

@davideast
Copy link
Member

We have plans to support this in the future, $ref is definitely worth having :)

katowulf added a commit to katowulf/angularfire2 that referenced this issue Aug 17, 2016
…observables

Makes the firebase.database.Reference used to create the observable public so that it can be used for accessing child records and paths relative to this collection/object.

This closes angular#294
@davideast davideast added this to the beta.5 milestone Aug 24, 2016
davideast pushed a commit that referenced this issue Sep 19, 2016
* feat(FirebaseObjectObservable, FirebaseArrayObservable): Add $ref to observables

Makes the firebase.database.Reference used to create the observable public so that it can be used for accessing child records and paths relative to this collection/object.

This closes #294

* Locked zone.js to 0.6.12 per bug angular/zone.js#404, bug #468 created to address this.
Commented test units to correct `error TS2339`; bug #467 created to address this.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants