<commit_before>Rahani.module 'Views.Goals', ->
  class @ListItem extends Backbone.Marionette.ItemView
    tagName: 'div'
    className: 'list-group-item'
    template: 'goals/list_item'

    ui:
      amount: 'input[name=amount]'
      period: 'select[name=period]'

    onRender: ->
      new Rahani.Helpers.FormErrors(@model, @$el)
      _.each RahaniData.goal_periods, (option)=>
        $option = $("<option value='#{option}'>#{option}</option>")
        @ui.period.append($option)

      @ui.amount.val(@model.get('amount'))
      @ui.period.val(@model.get('period'))

    events:
      'click button.save': 'save'
      'submit form': 'save'
      'change input': 'reRender'
      'change select': 'reRender'

    reRender: ->
      @update()
      @render()

    update: ->
      data =
        amount: @ui.amount.val()
        period: @ui.period.val()

      @model.set(data)

    save: ->
      @update()

      if @model.hasChanged()
        @model.save {},
        success: =>
          @trigger 'save', @model

      false
    templateHelpers: ->
      monthAmount: =>
        amount = switch @model.get('period')
          when 'Month'
            1 * @model.get('amount')
          when 'Fortnight'
            1 * @model.get('amount') * 13.0/6
          when 'Week'
            1 * @model.get('amount') * 13.0/3

        accounting.formatMoney(amount)<commit_msg>Manually track `hasChanged` on goal list items.<commit_after>Rahani.module 'Views.Goals', ->
  class @ListItem extends Backbone.Marionette.ItemView
    tagName: 'div'
    className: 'list-group-item'
    template: 'goals/list_item'

    ui:
      amount: 'input[name=amount]'
      period: 'select[name=period]'

    initialize: ->
      @hasChanged = false

    onRender: ->
      new Rahani.Helpers.FormErrors(@model, @$el)
      _.each RahaniData.goal_periods, (option)=>
        $option = $("<option value='#{option}'>#{option}</option>")
        @ui.period.append($option)

      @ui.amount.val(@model.get('amount'))
      @ui.period.val(@model.get('period'))

    events:
      'click button.save': 'save'
      'submit form': 'save'
      'change input': 'reRender'
      'change select': 'reRender'

    reRender: ->
      @update()
      @render()

    update: ->
      data =
        amount: @ui.amount.val()
        period: @ui.period.val()

      @model.set(data)

      if @model.hasChanged()
        @hasChanged = true

    save: ->
      @update()

      if @hasChanged
        @model.save {},
          success: =>
            @hasChanged = false
            @trigger 'save', @model

      false
    templateHelpers: ->
      monthAmount: =>
        amount = switch @model.get('period')
          when 'Month'
            1 * @model.get('amount')
          when 'Fortnight'
            1 * @model.get('amount') * 13.0/6
          when 'Week'
            1 * @model.get('amount') * 13.0/3

        accounting.formatMoney(amount)
{'fraction_non_alphanumeric': 0.16056145155768572, 'fraction_numerical': 0.007531667237247518, 'mean_word_length': 2.0757894736842104, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2141215', 'n_tokens_mistral': 951, 'n_tokens_neox': 921, 'n_words': 180}