BigW Consortium Gitlab

subscription.js.coffee 882 Bytes
Newer Older
Valery Sizov committed
1
class @Subscription
2 3 4
  constructor: (container) ->
    $container = $(container)
    @url = $container.attr('data-url')
5
    @subscribe_button = $container.find('.js-subscribe-button')
6 7
    @subscription_status = $container.find('.subscription-status')
    @subscribe_button.unbind('click').click(@toggleSubscription)
Valery Sizov committed
8

9 10
  toggleSubscription: (event) =>
    btn = $(event.currentTarget)
11 12
    action = btn.find('span').text()
    current_status = @subscription_status.attr('data-status')
13
    btn.addClass('disabled')
14 15

    $.post @url, =>
16
      btn.removeClass('disabled')
17 18 19 20 21
      status = if current_status == 'subscribed' then 'unsubscribed' else 'subscribed'
      @subscription_status.attr('data-status', status)
      action = if status == 'subscribed' then 'Unsubscribe' else 'Subscribe'
      btn.find('span').text(action)
      @subscription_status.find('>div').toggleClass('hidden')