codecombat/app/views/editor/patches_view.coffee
Ting-Kuan 692693c181 Merge branch 'master' into feature/loading-views
Conflicts:
	app/locale/en.coffee
	app/views/editor/level/edit.coffee
	app/views/kinds/CocoView.coffee
	app/views/play/ladder/ladder_tab.coffee
	app/views/play/ladder_view.coffee
	app/views/play/level/level_loading_view.coffee
2014-04-19 01:15:04 -04:00

62 lines
1.7 KiB
CoffeeScript

CocoView = require 'views/kinds/CocoView'
template = require 'templates/editor/patches'
PatchesCollection = require 'collections/PatchesCollection'
nameLoader = require 'lib/NameLoader'
PatchModal = require './patch_modal'
module.exports = class PatchesView extends CocoView
template: template
className: 'patches-view'
status: 'pending'
events:
'change .status-buttons': 'onStatusButtonsChanged'
'click .patch-icon': 'openPatchModal'
constructor: (@model, options) ->
super(options)
@initPatches()
initPatches: ->
@startedLoading = false
@patches = new PatchesCollection([], {}, @model, @status)
@patchesRes = @supermodel.addModelResource(@patches, 'patches')
@patchesRes.load()
load: ->
return unless @patchesRes.loaded
ids = (p.get('creator') for p in @patches.models)
jqxhrOptions = nameLoader.loadNames ids
@nameLoaderRes = @supermodel.addRequestResource('name_loader', jqxhrOptions)
@nameLoaderRes.load()
getRenderData: ->
c = super()
patch.userName = nameLoader.getName(patch.get('creator')) for patch in @patches.models
c.patches = @patches.models
c.status
c
onLoaded: -> @render()
afterRender: ->
@$el.find(".#{@status}").addClass 'active'
onStatusButtonsChanged: (e) ->
@status = $(e.target).val()
@reloadPatches()
reloadPatches: ->
@loaded = false
@initPatches()
@load()
@render()
openPatchModal: (e) ->
patch = _.find @patches.models, {id:$(e.target).data('patch-id')}
modal = new PatchModal(patch, @model)
@openModalView(modal)
@listenTo modal, 'accepted-patch', -> @trigger 'accepted-patch'
@listenTo modal, 'hide', ->
f = => @reloadPatches()
setTimeout(f, 400)
@stopListening modal