Skip to content

Instantly share code, notes, and snippets.

@katowulf
Last active October 7, 2015 02:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save katowulf/e8f35702a79eb9524e48 to your computer and use it in GitHub Desktop.
Save katowulf/e8f35702a79eb9524e48 to your computer and use it in GitHub Desktop.
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
Copy link

viisual commented Oct 6, 2015

<3

@viisual
Copy link

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
Copy link

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