1
votes

I am creating a webapp using AngularJS and Firebase, in order to learn both.

I have some user activity data in firebase (activitylog) which would not exist for new users. In the controller that adds activity data, I'm having trouble figuring out the best way to create a new user entry to log the new data. I have the following, and it seems there must be a better way, because it results in a 'dummy' entry that is never used later. My firebase is arranged such that activity logs are children of a user id, e.g. firebase/activitylog/[userid]/[activityId]/[activityEntry]. It doesn't have to be this way, just seems to be the best way to organize it for now. (Note I have a factory that handles firebase references called fbRef, per the FireFeed RSS open source project). It'd be great if calling angularFire() simply created a record if referencing something that doesn't exist, but it just gives an error. Any help would be appreciated.

.controller('ActivityLogCtrl', ['$scope', '$location', '$routeParams', '$timeout', 'angularFireCollection', 'angularFire', 'fbRef', function($scope, $location, $routeParams, $timeout, angularFireCollection, angularFire, fbRef) {

  //check if the user has a activitylog entry from the past, if not create a default one
  var ref = fbRef(['activitylog', $scope.user.id]);

  ref.on('value', function(snapshot) {
    if(snapshot.val() === null) {
        var wlref = fbRef(['activitylog']);
        // create a new dummy entry
        wlref.child($scope.user.id).set('initial');
    }
  });

  // now I know if have an entry, use it.
  angularFire(fbRef(['activitylog', $scope.user.id]), $scope, 'remote', {}).
  then(function() {

    $scope.activitylog = angular.copy($scope.remote);

    ...
1

1 Answers

0
votes

You can update to the latest version of AngularFire (0.3 at this time), available at https://cdn.firebase.com/libs/angularfire/0.3.0/angularfire.js - in this version binding to non-existent data will just work and the 4th argument is not required. For example:

var ref = new Firebase(URL);
$scope.remote = {};
angularFire(ref, $scope, "remote");

will work even if there is no data at URL.