Skip to content

Instantly share code, notes, and snippets.

@victorkurauchi
Last active January 2, 2016 15:18
Show Gist options
  • Save victorkurauchi/8322141 to your computer and use it in GitHub Desktop.
Save victorkurauchi/8322141 to your computer and use it in GitHub Desktop.
Angular Firebase Service
// In this case it is a simple value service.
angular.module('myApp.services', ['firebase']).
value('version', '0.1')
// Serviço criado para persistir dados no firebase,
// com a possibilidade de ser a propria persistencia em base de dados como mongodb etc..
.service('Firebase', function($rootScope, $firebase) {
return {
url: 'https://myurl.firebaseio.com',
path: '',
reference: null,
_construct: function(path) {
this.path = path;
var ref = new Firebase(this.url + '/' + path);
this.reference = $firebase(ref);
},
on: function(eventName, callback) {
var _this = this;
_this.reference.$on(eventName, function () {
var args = arguments;
$rootScope.$apply(function () {
callback.apply(_this.reference, args);
});
});
},
add: function(item) {
var _ref = this.reference;
_ref.push(item);
}
}
});
angular.module('myApp.services', []).
controller('FirebaseController', function ($scope, Firebase, $timeout) {
$scope.predicate = '';
Firebase._construct('tarifas');
Firebase.on('loaded', function(data) {
$timeout(function() {
console.log('waddup');
console.log(data);
//$scope.messages.push(data.val());
},0);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment