Skip to content

Instantly share code, notes, and snippets.

@sfarthin
Last active August 29, 2015 13:56
Show Gist options
  • Save sfarthin/8834003 to your computer and use it in GitHub Desktop.
Save sfarthin/8834003 to your computer and use it in GitHub Desktop.
AMD Angular Service for Firebase.com service
/**
* Lets make a service rather than refering to firebase directly
*
* Copied and tweeked from angularFire-seed.
* https://github.com/firebase/angularFire-seed/blob/master/app/js/service.firebase.js
*
* For use with bower.
* bower install https://gist.github.com/8834003.git --save
**/
define(["angularfire"], function() {
angular.module('syncData', ['firebase'])
.factory('firebaseRef', ['Firebase', 'fbURL', function(Firebase, fbURL) {
/**
* @function
* @name firebaseRef
* @param {String|Array...} path
* @return a Firebase instance
*/
return function(path) {
return new Firebase(pathRef([fbURL].concat(Array.prototype.slice.call(arguments))));
}
}])
// a simple utility to create $firebase objects from angularFire
.service('syncData', ['$firebase', 'firebaseRef', '$q', function($firebase, firebaseRef, $q) {
/**
* @function
* @name syncData
* @param {String|Array...} path
* @param {int} [limit]
* @return a Firebase instance
*/
return function(path, limit) {
// Lets grab our firebase reference and limit it
var ref = firebaseRef(path);
limit && (ref = ref.limit(limit));
var object = $firebase(ref);
/**
*
* Lets expose a very limited subset of the API
*
**/
console.log(object);
return {
/**
* @function
* @name $bind
* @param {Object} object
* @return a Promise
*/
$bind: object.$bind,
/**
* @function
* @name $add
* @param {Object} object
* @return a Promise
*/
$add: function() {
var deferred = $q.defer();
object.$add.apply(object, arguments).then(function(record) {
// API tweek, Return the id
deferred.resolve(record.name());
});
return deferred.promise;
}
};
}
}]);
function pathRef(args) {
for(var i=0; i < args.length; i++) {
if( typeof(args[i]) === 'object' ) {
args[i] = pathRef(args[i]);
}
}
return args.join('/');
}
});
@felicedeNigris
Copy link

So is this example using a service to connect to Firebase , in which you could connect controllers to your service Firebase object ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment