codecombat/app/views/modal/NewModelModal.coffee
2014-08-05 15:15:53 +02:00

48 lines
1.3 KiB
CoffeeScript

ModalView = require 'views/kinds/ModalView'
template = require 'templates/modal/new_model'
forms = require 'lib/forms'
module.exports = class NewModelModal extends ModalView
id: 'new-model-modal'
template: template
plain: false
events:
'click button.new-model-submit': 'makeNewModel'
'submit form': 'makeNewModel'
'shown.bs.modal #new-model-modal': 'focusOnName'
constructor: (options) ->
super options
@model = options.model
@modelLabel = options.modelLabel
@properties = options.properties
getRenderData: ->
c = super()
c.modelLabel = @modelLabel
#c.newModelTitle = @newModelTitle
c
makeNewModel: (e) ->
e.preventDefault()
name = @$el.find('#name').val()
model = new @model
model.set('name', name)
if @model.schema.properties.permissions
model.set 'permissions', [{access: 'owner', target: me.id}]
model.set(key, prop) for key, prop of @properties if @properties?
res = model.save()
return unless res
forms.clearFormAlerts @$el
@showLoading(@$el.find('.modal-body'))
res.error =>
@hideLoading()
forms.applyErrorsToForm(@$el, JSON.parse(res.responseText))
#Backbone.Mediator.publish 'model-save-fail', model
res.success =>
@$el.modal('hide')
@trigger 'success', model
#Backbone.Mediator.publish 'model-save-success', model