codecombat/app/views/editor/achievement/edit.coffee

75 lines
2 KiB
CoffeeScript
Raw Normal View History

View = require 'views/kinds/RootView'
ErrorView = require '../../error_view'
template = require 'templates/editor/achievement/edit'
Achievement = require 'models/Achievement'
module.exports = class AchievementEditView extends View
id: "editor-achievement-edit-view"
template: template
startsLoading: true
events:
'click #save-button': 'saveAchievement'
subscriptions:
'save-new': 'saveAchievement'
constructor: (options, @achievementID) ->
super options
@achievement = new Achievement(_id: @achievementID)
@achievement.saveBackups = true
@listenToOnce(@achievement, 'error',
() =>
@hideLoading()
$(@$el).find('.main-content-area').children('*').not('#error-view').remove()
@insertSubView(new ErrorView())
)
@achievement.fetch()
@listenToOnce(@achievement, 'sync', @buildTreema)
@pushChangesToPreview = _.throttle(@pushChangesToPreview, 500)
buildTreema: ->
return if @treema? or (not @achievement.loaded)
@startsLoading = false
@render()
data = $.extend(true, {}, @achievement.attributes)
options =
data: data
#filePath: "db/thang.type/#{@article.get('original')}"
schema: Achievement.schema
readOnly: me.get('anonymous')
callbacks:
change: @pushChangesToPreview
@treema = @$el.find('#achievement-treema').treema(options)
@treema.build()
pushChangesToPreview: =>
'TODO' # TODO might want some intrinsic preview thing
getRenderData: (context={}) ->
context = super(context)
context.achievement = @achievement
context.authorized = me.isAdmin()
context
openSaveModal: ->
'Maybe later' # TODO
saveAchievement: (e) ->
@treema.endExistingEdits()
for key, value of @treema.data
@achievement.set(key, value)
res = @achievement.save()
res.error =>
console.log 'Failed to save achievement'
res.success =>
url = "/editor/achievement/#{@achievement.get('slug') or @achievement.id}"
document.location.href = url