Skip to content

Instantly share code, notes, and snippets.

@simonecorsi
Created May 6, 2015 12:09
Show Gist options
  • Select an option

  • Save simonecorsi/e272193b7736547d05ad to your computer and use it in GitHub Desktop.

Select an option

Save simonecorsi/e272193b7736547d05ad to your computer and use it in GitHub Desktop.
AngularFire Item Management Factory
'use strict';
// Remenber to add firebase/AngularFire scripts in you index.html
// FB_URL define .constant("FB_URL", "http://<firebase>.firebase.io")
angular.module("ItemFactory", [])
.factory("ItemData",function(FB_URL, $firebase, Auth){
var ref = new Firebase(FB_URL);
//get all task in array
var items = $firebase(ref.child("ITEMS")).$asArray();
//get current user data
var user = Auth.user;
var ItemData = {
all: items,
getItem: function(itemId){
//function return link to the object in the database
return $firebase(ref.child("ITEMS").child(itemId));
},
createItem: function(item){
item.datetime = Firebase.ServerValue.TIMESTAMP;
return items.$add(item);
},
editItem: function(item){
//link t the object in the database with this $id
var t = this.getItem(item.$id);
return t.$update({ title: item.title, description: item.description, total: item.total });
},
cancelItem: function(item){
var t = this.getItem(item.$id);
return t.$update({ status:"cancelled" });
},
isCreator: function(item){
// to see if the logged user is the creator of a post to make em edit easily
return (user && user.provider && user.uid === item.poster);
},
isOpen: function(item){
return item.status === "open";
}
};
return ItemData;
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment