codecombat/app/views/modal/ModelModal.coffee

72 lines
2.6 KiB
CoffeeScript
Raw Normal View History

ModalView = require 'views/core/ModalView'
template = require 'templates/modal/model-modal'
require 'vendor/treema'
2014-07-17 20:22:26 -04:00
module.exports = class ModelModal extends ModalView
id: 'model-modal'
template: template
2014-06-13 13:11:01 -04:00
plain: true
events: 'click .save-model': 'onSaveModel'
constructor: (options) ->
super options
@models = options.models
for model in @models when not model.loaded
2016-02-18 11:44:40 -05:00
@supermodel.loadModel model
model.fetch cache: false
afterRender: ->
return unless @supermodel.finished()
@modelTreemas = {}
for model in @models
data = $.extend true, {}, model.attributes
schema = $.extend true, {}, model.schema()
treemaOptions =
schema: schema
data: data
readOnly: false
modelTreema = @$el.find(".model-treema[data-model-id='#{model.id}']").treema treemaOptions
modelTreema?.build()
modelTreema?.open()
@openTastyTreemas modelTreema, model
@modelTreemas[model.id] = modelTreema
openTastyTreemas: (modelTreema, model) ->
# To save on quick inspection, let's auto-open the properties we're most likely to want to see.
delicacies = ['code']
for dish in delicacies
child = modelTreema.childrenTreemas[dish]
child?.open()
if child and dish is 'code' and model.type() is 'LevelSession' and team = modelTreema.get('team')
desserts = {
humans: ['programmable-tharin', 'programmable-librarian']
ogres: ['programmable-brawler', 'programmable-shaman']
}[team]
for dessert in desserts
child.childrenTreemas[dessert]?.open()
onSaveModel: (e) ->
container = $(e.target).closest('.model-container')
model = _.find @models, id: container.data('model-id')
treema = @modelTreemas[model.id]
for key, val of treema.data when not _.isEqual val, model.get key
2014-06-30 22:16:26 -04:00
console.log 'Updating', key, 'from', model.get(key), 'to', val
model.set key, val
for key, val of model.attributes when treema.get(key) is undefined and not _.string.startsWith key, '_'
2014-06-30 22:16:26 -04:00
console.log 'Deleting', key, 'which was', val, 'but man, that ain\'t going to work, now is it?'
#model.unset key
if errors = model.validate()
2014-06-30 22:16:26 -04:00
return console.warn model, 'failed validation with errors:', errors
return unless res = model.patch()
res.error =>
return if @destroyed
2014-06-30 22:16:26 -04:00
console.error model, 'failed to save with error:', res.responseText
res.success (model, response, options) =>
return if @destroyed
@hide()
destroy: ->
@modelTreemas[model].destroy() for model of @modelTreemas
super()