I'm going through a Firebase tutorial using angular and it started by making child_added requests in a controller, but now we're refactoring and moving those requests to a service -- as we should.
However, there's some syntax being used in the service that I'm not familiar with. Here's my service that's making a child_added request to the firebase db:
app.service(‘messageService’, function() {
var messagesRef = new Firebase(FBURL);
return {
childAdded: function childAdded(callback) {
messagesRef.on(‘child_added’, function(data) {
callback.call(this, {
user: data.val().user,
text: data.val().text,
name: data.name()
});
});
}
}
}
Then in the controller we're using the service like so:
messageService.childAdded(function(addedChild) {
$timeout(function() {
$scope.messages.push(addedChild);
});
});
Firstly, I'm confused as to what callback.call(this, ... is doing? Secondly, how does that line tie in with push() method in the controller?
I would greatly appreciate any explanations. Thanks!