2014-05-21 07:52:09 -04:00
|
|
|
View = require 'views/kinds/RootView'
|
|
|
|
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:
|
2014-05-21 13:47:17 -04:00
|
|
|
'click #save-button': 'saveAchievement'
|
2014-05-21 07:52:09 -04:00
|
|
|
|
|
|
|
subscriptions:
|
2014-05-21 13:47:17 -04:00
|
|
|
'save-new': 'saveAchievement'
|
2014-05-21 07:52:09 -04:00
|
|
|
|
|
|
|
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
|
2014-05-22 10:59:01 -04:00
|
|
|
filePath: "db/achievement/#{@achievement.get('_id')}"
|
2014-05-21 07:52:09 -04:00
|
|
|
schema: Achievement.schema
|
|
|
|
readOnly: me.get('anonymous')
|
|
|
|
callbacks:
|
|
|
|
change: @pushChangesToPreview
|
|
|
|
@treema = @$el.find('#achievement-treema').treema(options)
|
|
|
|
|
|
|
|
@treema.build()
|
|
|
|
|
|
|
|
pushChangesToPreview: =>
|
2014-05-21 13:47:17 -04:00
|
|
|
'TODO' # TODO might want some intrinsic preview thing
|
2014-05-21 07:52:09 -04:00
|
|
|
|
|
|
|
getRenderData: (context={}) ->
|
|
|
|
context = super(context)
|
|
|
|
context.achievement = @achievement
|
|
|
|
context.authorized = me.isAdmin()
|
|
|
|
context
|
|
|
|
|
|
|
|
openSaveModal: ->
|
2014-05-21 13:47:17 -04:00
|
|
|
'Maybe later' # TODO
|
2014-05-21 07:52:09 -04:00
|
|
|
|
|
|
|
saveAchievement: (e) ->
|
2014-05-21 13:47:17 -04:00
|
|
|
@treema.endExistingEdits()
|
|
|
|
for key, value of @treema.data
|
|
|
|
@achievement.set(key, value)
|
|
|
|
|
|
|
|
res = @achievement.save()
|
|
|
|
|
2014-05-31 17:55:26 -04:00
|
|
|
res.error (collection, response, options) =>
|
|
|
|
console.error response
|
2014-05-21 13:47:17 -04:00
|
|
|
|
|
|
|
res.success =>
|
|
|
|
url = "/editor/achievement/#{@achievement.get('slug') or @achievement.id}"
|
2014-05-31 17:19:55 -04:00
|
|
|
document.location.href = url
|