codecombat/app/views/editor/PatchesView.coffee

65 lines
1.8 KiB
CoffeeScript
Raw Normal View History

CocoView = require 'views/core/CocoView'
2014-04-11 17:19:17 -04:00
template = require 'templates/editor/patches'
PatchesCollection = require 'collections/PatchesCollection'
nameLoader = require 'core/NameLoader'
PatchModal = require './PatchModal'
2014-04-11 17:19:17 -04:00
module.exports = class PatchesView extends CocoView
template: template
className: 'patches-view'
status: 'pending'
2014-04-11 17:19:17 -04:00
events:
'change .status-buttons': 'onStatusButtonsChanged'
'click .patch-row': 'openPatchModal'
2014-04-11 17:19:17 -04:00
constructor: (@model, options) ->
super(options)
@initPatches()
2014-04-11 17:19:17 -04:00
initPatches: ->
@startedLoading = false
@patches = @model.fetchPatchesWithStatus()
load: ->
@initPatches()
@patches = @model.fetchPatchesWithStatus(@status, {cache: false})
@supermodel.trackCollection(@patches)
@listenTo @patches, 'sync', @onPatchesLoaded
onPatchesLoaded: ->
2014-04-11 17:19:17 -04:00
ids = (p.get('creator') for p in @patches.models)
jqxhrOptions = nameLoader.loadNames ids
2014-04-28 14:10:27 -04:00
@supermodel.addRequestResource('user_names', jqxhrOptions).load() if jqxhrOptions
2014-04-11 17:19:17 -04:00
getRenderData: ->
c = super()
patch.userName = nameLoader.getName(patch.get('creator')) for patch in @patches.models
c.patches = @patches.models
c.status
c
2014-04-11 17:19:17 -04:00
afterRender: ->
@$el.find(".#{@status}").addClass 'active'
super()
2014-04-11 17:19:17 -04:00
onStatusButtonsChanged: (e) ->
@status = $(e.target).val()
2014-04-17 18:44:19 -04:00
@reloadPatches()
2014-04-17 18:44:19 -04:00
reloadPatches: ->
@supermodel.resetProgress()
@load()
2014-04-11 17:19:17 -04:00
@render()
2014-04-12 00:11:52 -04:00
openPatchModal: (e) ->
row = $(e.target).closest '.patch-row'
patch = _.find @patches.models, {id: row.data('patch-id')}
2014-04-12 00:11:52 -04:00
modal = new PatchModal(patch, @model)
@openModalView(modal)
2014-04-17 18:44:19 -04:00
@listenTo modal, 'accepted-patch', -> @trigger 'accepted-patch'
@listenTo modal, 'hide', ->
f = => @reloadPatches()
setTimeout(f, 400)
@stopListening modal