A
A
aTei2013-12-26 17:37:11
CoffeeScript
aTei, 2013-12-26 17:37:11

How to get rid of duplication?

I understand that duplication is very bad. But there are absolutely no ideas how to get rid of it here.
Any actions with follow and unfollow create almost identical code.
Which approach to use? Which way to look?

class UserFollows

  constructor: (@container) ->
    @user_id = @container.data 'user-id'
    @get_follow_status()
    @container.find('.follow-link').on 'click', @follow
    @container.find('.unfollow-link').on 'click', @unfollow

  get_follow_status: ->
    $.post "/u/#{@user_id}/already_following", (data) ->
      if data == 'yes'
        @set_unfollow()
      else
        @set_follow()

  follow: (e) =>
    e.preventDefault()
    $.post "/u/#{@user_id}/follow", ->
      @set_follow()

  unfollow: (e) =>
    e.preventDefault()
    $.post "/u/#{@user_id}/unfollow", ->
      @set_unfollow()


  set_follow: ->
    @container.removeClass 'follow-container_unfollow'
    @container.addClass 'follow-container_follow'


  set_unfollow: ->
    @container.removeClass 'follow-container_follow'
    @container.addClass 'follow-container_unfollow'

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question