Xiaofan Wu
Xiaofan Wu

Reputation: 119

Prevent Firebase $firebaseArray auto update

I am working on a News Feed that is similar to Facebook News Feed. I am implementing it in the following sequence:

  1. When app is started, get everything, but only push two item to show.

  2. The user can scroll down to see more and I will add more to the list.

  3. If there are any new items, the user needs to pull to refresh to add the new ones.

The only problem I have right now is that each time an item is added, $firebaseArray automatically adds the new item to my array that I do not want snyc changes (I want to query once, get a set list). I was wondering if anyone would know how to prevent the auto update?

Upvotes: 0

Views: 512

Answers (2)

Mathew Berg
Mathew Berg

Reputation: 28750

Look into .once this will allow you to get the data just once: https://firebase.google.com/docs/reference/js/firebase.database.Query#once

Upvotes: 1

vgrafe
vgrafe

Reputation: 1350

Instead of binding directly your $firebaseArray to the view, clone it to a scope (or controller) property that will be bound to the view. Factorize this process in a function you can call when the user pull-reloads so the the $firebaseArray is re-created.

A more appropriate approach would be to use Firebase's http api, with the angular $http service.

Edit: to clone the array to a scope var, try the following:

var allEvents = $firebaseArray(queryOfeverything);

allEvents.$loaded()
    .then(function(events){
        $scope.allEvents = angular.map(events, function(event) {
            return event; // you may want to replace this line - I am not very familiar with firebase.
        })
    });

Upvotes: 0

Related Questions