Skip to content

Instantly share code, notes, and snippets.

@ys
Created November 22, 2012 08:57
Show Gist options
  • Save ys/4130082 to your computer and use it in GitHub Desktop.
Save ys/4130082 to your computer and use it in GitHub Desktop.
StoreQuest.Views.DispatchedFaces ||= {}
class StoreQuest.Views.DispatchedFaces.ListView extends Backbone.View
face_views: []
attribute: 'selected'
initialize: (options) ->
@set_local_options(options)
@base_url = options['base_url']
@parent = options['parent']
@attribute = options['attribute'] if options['attribute']?
@face_views = (new StoreQuest.Views.DispatchedFaces.FaceView(el: face_row, parent: @, toggled_attribute: @attribute, base_url: @base_url) for face_row in @$('tr.face-row'))
@$('.select-all').on 'click', (e) => @toggle_all(e, true)
@$('.unselect-all').on 'click', (e) => @toggle_all(e, false)
toggle_all: (e, status) ->
$select_btn = @$('.select-btn')
post_params = { _method: 'PUT', toggled_attribute: @attribute}
@add_filter_values post_params
$.ajax
type: 'POST'
dataType: 'json'
data:
post_params
url: "#{$select_btn.attr('data-url')}/#{if status then 'select' else 'unselect'}"
success: (data) =>
@success(e, status, data)
$('.dropdown-toggle', $select_btn).dropdown('toggle')
false
success: (e, status, data) ->
trs = $(e.currentTarget).parents('table').find('tr')
for face_view in @face_views when face_view.el in trs
if status
face_view.selected()
else
face_view.unselected()
@update_face_count(data.count)
update_face_count: (count) ->
$tab = $("a[href='##{@$el.attr('id')}']")
word_count_regex = /\(\d+\/(.*)\)/
$tab.html($tab.html().replace(word_count_regex,"(#{count}/$1)"))
set_local_options: (options) ->
add_filter_values: (hash) ->
@parent.$el.find('select').each (index, select) -> hash[$(select).attr('name')] = $(select).val()
hash
on_close: ->
face_view.close() for face_view in @face_views
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment