Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use angularfire $destroy()?

Tags:

angularfire

What is the reason that $destroy() exists in Angularfire?

The documentation of angularfire sais:

https://www.firebase.com/docs/web/libraries/angular/api.html#angularfire-firebasearray-destroy

Stop listening for events and free memory used by this array (empties the local copy). Changes are no longer synchronized to or from Firebase.

sync = $firebase(ref).$asArray();
...
....
sync.$destroy()

Can I not just do:

sync = null

or

delete sync

Or should I really use $destroy() for some reason?

like image 214
Dirk Avatar asked Jan 28 '15 15:01

Dirk


1 Answers

The $destroy() exists to empty the data and unbind event listeners. If you need to unbind the $firebaseArray() or $firebaseObject() you could use $destroy() but I think it would be nicer to use the unbind function that is resolved.

This was code snippet was taken from angularfire-seed

  var unbind;
  // create a 3-way binding with the user profile object in Firebase
  var profile = $firebaseObject(fbutil.ref('users', user.uid));
  profile.$bindTo($scope, 'profile').then(function(ub) { unbind = ub; });

  // expose logout function to scope
  $scope.logout = function() {
    if( unbind ) { unbind(); }
    profile.$destroy();
    ...
  };
like image 106
Cory Silva Avatar answered Oct 10 '22 09:10

Cory Silva