Skip to content

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

@DennisSmolek

Description

@DennisSmolek

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions