Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
app.factory('JoinedUser', function($firebaseObject) {
return $firebaseObject.$extend({
$$updated: function(snapshot) {
if( !this.profile ) {
// back up and get a reference to the profile path
var ref = snapshot.ref().parent().child('profile/' + snapshot.key());
this.profile = $firebaseObject(ref);
}
},
/*
* If we want to automatically save the profile when saving the user object, we could uncomment this
$save: function() {
if( this.profile ) {
// we save this blindly and don't handle errors or wait for results here
this.profile.$save();
}
return $firebaseObject.prototype.$save.call(this);
},
*/
$destroy: function() {
if( this.profile ) {
// stop listening for changes to the profile
this.profile.$destroy();
}
$firebaseObject.prototype.$destroy.call(this);
}
toJSON: function() {
// don't returt the profile when saving this object
var json = $firebaseObject.prototype.toJSON.apply(this, arguments);
delete json.profile;
return json;
}
});
});
// usage
app.controller('UseTheService', function($scope, JoinedUser) {
var ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com/").child('users/kato');
$scope.user = new JoinedUser(ref);
});
@viisual

This comment has been minimized.

Copy link

@viisual viisual commented Oct 6, 2015

<3

@viisual

This comment has been minimized.

Copy link

@viisual viisual commented Oct 7, 2015

Very nice.

I came across your work just after this actually here (i think):https://www.firebase.com/blog/2013-10-01-queries-part-one.html#join

Perhaps I'm just not storing my data efficiently, but would this be appropriate for selecting a value on 'users' as the uid for 'profiles':

    function updated(snapshot) {
      if(!this.profile) {
        // back up and get a reference to the profile path
        var ref = snapshot.ref().parent().child('profiles/' + snapshot.val().profileId);
        this.profile = $firebaseObject(ref);
      }
    }
@viisual

This comment has been minimized.

Copy link

@viisual viisual commented Oct 7, 2015

Yea dude, thanks so much for taking the time out of your day to educate me; I'm very appreciative of that, catch you tomorrow! ^_^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment