62 lines
2.5 KiB
CoffeeScript
62 lines
2.5 KiB
CoffeeScript
lunchControllers = angular.module 'lunchControllers'
|
|
|
|
lunchControllers.controller 'EditRunCtrl', ['$scope', 'socket', '$routeParams', '$sce', '$location',
|
|
EditRunController = ($scope, socket, $routeParams, $sce, $location) ->
|
|
$scope.show = false
|
|
|
|
socket.get '/run/' + $routeParams['id'],
|
|
(response) ->
|
|
if response.status is 404
|
|
window.location = '/404'
|
|
return
|
|
|
|
$scope.show = true
|
|
$scope.run = response
|
|
|
|
socket.get '/invitee', run_id: response.id, (inviteeResponse) ->
|
|
$scope.invitees = inviteeResponse
|
|
$scope.$apply()
|
|
|
|
$scope.addInvitee = () ->
|
|
$scope.invitees.push {}
|
|
|
|
$scope.removeInvitee = (invitee) ->
|
|
if invitee.id
|
|
socket.delete '/invitee/destroy/' + invitee.id, (deleteResponse) ->
|
|
console.log deleteResponse
|
|
$scope.spliceInvitee invitee
|
|
else
|
|
$scope.spliceInvitee invitee
|
|
|
|
$scope.spliceInvitee = (invitee) ->
|
|
index = $scope.invitees.indexOf invitee
|
|
$scope.invitees.splice index, 1
|
|
$scope.$apply()
|
|
|
|
$scope.update = (run,invitees) ->
|
|
runData = angular.copy run
|
|
inviteeData = angular.copy invitees
|
|
creatorInvitee = _.find inviteeData, (i) -> return i.isCreator
|
|
creatorInvitee.name = runData.creatorName
|
|
creatorInvitee.email = runData.creatorEmail
|
|
|
|
socket.put '/run/' + runData.id, runData, (runResponse) ->
|
|
|
|
inviteesUpdated = 0
|
|
for i in inviteeData
|
|
if i.id
|
|
socket.put '/invitee/' + i.id, i, (inviteeResponse) ->
|
|
inviteesUpdated++
|
|
$scope.redirectIfAllInviteesUpdated inviteesUpdated, inviteeData.length
|
|
else
|
|
i.run_id = runData.id
|
|
socket.post '/invitee', i, (inviteeResponse) ->
|
|
inviteesUpdated++
|
|
$scope.redirectIfAllInviteesUpdated inviteesUpdated, inviteeData.length
|
|
|
|
$scope.redirectIfAllInviteesUpdated = (updatedCount, inviteeCount) ->
|
|
if updatedCount is inviteeCount
|
|
$location.path '/run/' + $scope.run.id
|
|
$scope.$apply()
|
|
]
|