Pregunta

I am new to firebase. I want to build it with angularjs and I found angularfire.

In the angularFire docs listed Implicit and explicit sync. I tried to understand the document in github but I still don't understand what is the difference and how to use them. angularFire() and angularFireCollection()

also, what are the arguments mean in angularFire() and angularFireCollection()?

thank in adv

¿Fue útil?

Solución

Use angularFire if you want implicit sync, i.e. any changes made to your model will instantly propagate to all other clients (and vice versa).

Use angularFireCollection if you want to be in control of when any local data changes must be sent to the server. Any remote changes will still automatically update your local collection.

Implicit sync:

myapp.controller('MyCtrl', ['$scope', 'angularFire',
  function MyCtrl($scope, angularFire) {
    var promise = angularFire(url, $scope, 'items', []);
  }
]);

First argument is the location of the Firebase at which you want to store/retrieve data. Second argument is the scope, third argument is the name of the property under $scope you want the data bound as soon as the promise is fulfilled. For example:

promise.then(function() {
  // Data available in $scope.items
});

Fourth argument is the type of data you want in your JS object. Use [] for arrays, {} for objects, "" for strings, 1 for numbers and true for boolean. Note that if no data is present in the provided Firebase location you can also use this argument to set the default value.

In implicit sync if you want to make any changes, simply modify $scope.items and the change will automatically synchronize with all other clients via Firebase. Similarly, any changes made remotely will automatically update $scope.items.

Explicit sync:

myapp.controller('MyCtrl', ['$scope', 'angularFireCollection',
  function MyCtrl($scope, angularFireCollection) {
    $scope.items = angularFireCollection(url);
  }
]);

This method only takes one argument. If you want to add or remove items, use the add, remove or update methods. For example:

$scope.items.add({test: "object"});

Since angularFireCollection does not require a scope, you can also use this if you want to use Firebase outside of a controller (like angular directives, modules, etc.) Hope this helps!

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top