Skip to content

Instantly share code, notes, and snippets.

@Piioo
Last active August 29, 2015 14:06
Show Gist options
  • Save Piioo/a8ff6e4cec5ec683c521 to your computer and use it in GitHub Desktop.
Save Piioo/a8ff6e4cec5ec683c521 to your computer and use it in GitHub Desktop.
Promise problem
angular.module('tbApp').controller 'NewTimeEntryController', ['$element', '$scope', '$http', ($element, $scope, $http) ->
newTimeEntryController = @
newTimeEntryController.formSubmit = (form) ->
TimeEntry.create($scope.timeEntryForm.data(), $scope.day, $http).then(newTimeEntryController.postSuccess, newTimeEntryController.postError)
newTimeEntryController.postSuccess = (response) ->
console.log(response.data)
$scope.day.addTimeEntry(response.data)
newTimeEntryController.destroy()
newTimeEntryController.postError = (response) ->
$.each(response.data.time_entry, (key, value) ->
if(key == 'project_code' || key == 'project_type')
key = 'project_id'
$scope.newForm[key].$dirty = true
$scope.newForm[key].$setValidity('required', false)
)
]
class @TimeEntry
constructor: (attributes, http) ->
@http = http
@initialize(attributes)
initialize: (attributes) ->
@id = attributes.id
@start_date = new Date(attributes.start_date)
@break_duration = attributes.break_duration
@confirmed = attributes.confirmed
@description = attributes.description
@duration = attributes.duration
@end_date = new Date(attributes.end_date)
@project = attributes.project || attributes.project_id
@start_time = attributes.start_time
@end_time = attributes.end_time
@create: (data, day, @http) ->
@http.post Locale.localized_path('/time_entries.json'),
time_entry: data
year: day.current_date.getFullYear()
month: day.current_date.getMonth() + 1
day: day.day_of_month
.success (timeEntryData) =>
timeEntry = new TimeEntry(timeEntryData)
console.log(timeEntry)
return timeEntry
.error (data) ->
alert('implement me in time_entry.js.coffee')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment