Created
February 25, 2015 01:38
-
-
Save jonpecson/623044d63b9860bcc6a5 to your computer and use it in GitHub Desktop.
firebase-service
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
app.controller('EventSignupController', function ($scope, $routeParams, EventService, AuthService) { | |
// Load the selected event with firebase through the eventservice | |
$scope.selectedEvent = EventService.events.get($routeParams.eventId); | |
// get user settings | |
$scope.user = AuthService.user; | |
$scope.signedIn = AuthService.signedIn; | |
// Message functionality | |
$scope.posts = EventService.posts.all($scope.selectedEvent.$id); | |
$scope.post = { | |
message: '' | |
}; | |
$scope.addPost = function (){ | |
$scope.post.creator = $scope.user.profile.username; | |
$scope.post.creatorUID = $scope.user.uid; | |
EventService.posts.createPost($scope.selectedEvent.$id, $scope.post); | |
}; | |
$scope.deletePost = function(post){ | |
EventService.posts.deletePost($scope.selectedEvent.$id, post); | |
// workaround for eventService bug: | |
// $scope.posts.$remove(post); | |
}; | |
}); |
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
app.factory('EventService', function ($firebase, FIREBASE_URL) { | |
var ref = new Firebase(FIREBASE_URL); | |
var events = $firebase(ref.child('events')).$asArray(); | |
var EventService = { | |
events: { | |
all: events, | |
create: function (event) { | |
return events.$add(event); | |
}, | |
get: function (eventId) { | |
return $firebase(ref.child('events').child(eventId)).$asObject(); | |
}, | |
delete: function (event) { | |
return events.$remove(event); | |
} | |
}, | |
posts: { | |
all: function(eventId){ | |
var posts = $firebase(ref.child('events').child(eventId).child('posts')).$asArray(); | |
return posts; | |
}, | |
createPost: function (eventId, post) { | |
// this does work | |
var posts = $firebase(ref.child('events').child(eventId).child('posts')).$asArray(); | |
return posts.$add(post); | |
}, | |
deletePost: function (eventId, post) { | |
// this does not work | |
var posts = $firebase(ref.child('events').child(eventId).child('posts')).$asArray(); | |
return posts.$remove(post); | |
} | |
} | |
}; | |
return EventService; | |
}); |
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
<div class="col-xs-8 col-xs-offset-2 well"> | |
<form ng-submit="addPost()" ng-show="signedIn()"> | |
<input type="text" ng-model="post.message" /> | |
<button type="submit" class="btn btn-primary btn-sm">Add Post</button> | |
</form> | |
<br> | |
<div class="post row" ng-repeat="post in posts"> | |
<div> | |
<div class="info"> | |
{{ post.message }} | |
</div> | |
<div> | |
<span>submitted by {{ post.creator }}</span> | |
<a href="" ng-click="deletePost(post)" ng-show="user.uid === post.creatorUID">delete</a> | |
</div> | |
<br> | |
</div> | |
</div> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment