Created
February 7, 2014 02:58
-
-
Save bmease/8856752 to your computer and use it in GitHub Desktop.
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
class EventCreateView extends Backbone.View | |
id: 'overlay' | |
initialize: -> | |
@template = _.template($('#template-event-create').html()) | |
@render() | |
events: { | |
'submit form': 'submit' | |
'click .cancel': 'close' | |
} | |
submit: (event) -> | |
event.preventDefault() | |
fields = @$el.find('form').serializeArray() | |
formObj = {} | |
for field in fields | |
# Catch start:time and end:time and convert them to datetimes | |
if field.name.indexOf(':time') isnt -1 | |
# get the parent name used in the formObj | |
parentName = field.name.split(':time').shift() | |
datetime = timeStrToDateStr(field.value, @model.get(parentName)) | |
formObj[parentName] = datetime | |
else | |
formObj[field.name] = field.value | |
@model.set(formObj) | |
@save() | |
save: => | |
@model.save null, | |
success: (model, response, options) => | |
@close() | |
console.debug 'Saved:', model, response, options | |
error: (model, xhr, options) => | |
console.error 'Failed saving', model, xhr, options | |
close: => | |
if @model.isNew() | |
@model.destroy() | |
@remove() | |
render: -> | |
templateVars = @model.toJSON() | |
templateVars.startTime = @model.getStartTime() | |
templateVars.endTime = @model.getEndTime() | |
form = @template(templateVars) | |
@$el.html(form) | |
return this | |
window.EventCreateView = EventCreateView |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment