Last active
August 29, 2015 13:56
-
-
Save sfarthin/8834003 to your computer and use it in GitHub Desktop.
AMD Angular Service for Firebase.com service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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('/'); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So is this example using a service to connect to Firebase , in which you could connect controllers to your service Firebase object ?