0

Consider a set of items and users. Each user can have one or more items. The set of items can be quite big, but every user will normally have small amount of items.

app:
  items:
    item1:
      name: 'table'
      color: 'white'
      createdAt: '2014-08-09T12:54:58.803Z'
    item2:
      name: 'macbook air'
      color: 'silver'
      createdAt: '2014-06-09T12:54:58.803Z'
    item3:
      name: 'firebase t-shirt'
      color: 'yellow'
      createdAt: '2014-07-09T12:54:58.803Z'
  users:
    user1:
      items:
        item1: true
        item3: true
    user2:
      items:
        item2: true

Given a user id, e.g. user1, I would like to display a list of user's items, sorted by createdAt:

yellow firebase t-shirt
white table

Every update on the Firebase server should be reflected in the app view.

I guess the view should look like this:

<div ng-repeat="item in items | orderBy:'createdAt'">
  {{ item.color }} {{ item.name }}
</div>

But, I can't figure out an easy way to set up $scope.items.

This is what I currently do:

var userItemsRef = new Firebase(FIREBASE_ROOT + '/users/user1/items');
$scope.userItems = $firebase(userItemsRef).$asArray();

$scope.userItems.$loaded(function() {
  $scope.userItems.$watch(setItems);
  setItems();
});

function setItems() {
  var promises = $scope.userItems.map(function(userItem) {
    var itemRef = new Firebase(FIREBASE_ROOT + '/items/' + userItem.$id);

    return $firebase(itemRef).$asObject().$loaded();
  });

  $q.all(promises).then(function(items) {
    $scope.items = items;
  });
}

Is this the best way to utilize AngularFire?

2
  • Are items shared between multiple users or is each item unique and assigned to exactly one user? Commented Aug 10, 2014 at 14:13
  • @Kato Each item is assigned to exactly one user. Commented Aug 10, 2014 at 22:05

1 Answer 1

2

It would probably be simplest to store the items by user. So a structure like this:

/app/items/$user_id/$item_id/...

This would allow for the items belonging to a particular user to be retrieved like so:

var userId = 'user1';
var ref = new Firebase(FIREBASE_ROOT + '/items/' + userId);
$scope.items = $firebase(ref).$asArray();

If this isn't possible, because items are shared between users, it's probably simplest to use a join lib.

var userId = 'user1';
var userIndexRef = new Firebase(FIREBASE_ROOT + '/users/' + userId + '/items');
var itemsRef = new Firebase(FIREBASE_ROOT + '/items/');
var ref = Firebase.util.intersection(userIndexRef, itemsRef);
$scope.items = $firebase(ref).$asArray();

But generally, it's safest and simplest to take the first route.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Kato, firebase-util looks interesting, I'll have a look!
It's handy, but because it's client side, it's not nearly as clean or fast as just storing the items per-user.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.