Created
May 6, 2015 12:09
-
-
Save simonecorsi/e272193b7736547d05ad to your computer and use it in GitHub Desktop.
AngularFire Item Management Factory
This file contains hidden or 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
| '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