codecombat/app/views/courses/ClassroomSettingsModal.coffee

52 lines
1.7 KiB
CoffeeScript
Raw Normal View History

Classroom = require 'models/Classroom'
ModalView = require 'views/core/ModalView'
template = require 'templates/courses/classroom-settings-modal'
2016-03-30 16:57:19 -04:00
forms = require 'core/forms'
errors = require 'core/errors'
module.exports = class ClassroomSettingsModal extends ModalView
id: 'classroom-settings-modal'
template: template
events:
2016-03-30 16:57:19 -04:00
'click #save-settings-btn': 'onSubmitForm'
'submit form': 'onSubmitForm'
2016-03-30 16:57:19 -04:00
initialize: (options={}) ->
@classroom = options.classroom or new Classroom()
if @classroom.isNew()
2015-12-04 17:19:56 -05:00
application.tracker?.trackEvent 'Create new class', category: 'Courses'
2016-03-30 16:57:19 -04:00
else
application.tracker?.trackEvent 'Classroom started edit settings', category: 'Courses', classroomID: @classroom.id
afterRender: ->
super()
2016-03-30 16:57:19 -04:00
forms.updateSelects(@$('form'))
onSubmitForm: (e) ->
@classroom.notyErrors = false
e.preventDefault()
form = @$('form')
forms.clearFormAlerts(form)
attrs = forms.formToObject(form)
if attrs.language
attrs.aceConfig = { language: attrs.language }
delete attrs.language
else
forms.setErrorToProperty(form, 'language', $.i18n.t('common.required_field'))
return
@classroom.set(attrs)
schemaErrors = @classroom.getValidationErrors()
if schemaErrors
forms.applyErrorsToForm(form, schemaErrors)
return
2016-03-30 16:57:19 -04:00
button = @$('#save-settings-btn')
@oldButtonText = button.text()
button.text($.i18n.t('common.saving')).attr('disabled', true)
@classroom.save()
2016-03-30 16:57:19 -04:00
@listenToOnce @classroom, 'error', (model, jqxhr) ->
@stopListening @classroom, 'sync', @hide
button.text(@oldButtonText).attr('disabled', false)
errors.showNotyNetworkError(jqxhr)
@listenToOnce @classroom, 'sync', @hide