0
votes

Ok so I am trying to save a client form to server. I am using boostrap modal to store the form and underscore template to get the for from backbone. After the form is submited its all good and well the form goes to backend and is saved in the database. The problem I'm having is that after I saved the client and want to input new info for another client it send a PUT method witch i solved by using "@model.clear()". Now every thing still works but I'm getting a "Uncaught ReferenceError: company is not defined" error in the console. and i know it is because of the ".clear()" though i have no idea how to fix it.

class Info.View.Client extends Backbone.View
 el: '.saveClient'

 events:
  'click .save': 'saveClient'
  'click .onClose': 'alertRemove'

 initialize: ()->
    Backbone.Validation.bind( @,
    {
    valid: (view, attr)->
      $('.company').removeClass('has-error')
      $('.address').removeClass('has-error')
      $('.city').removeClass('has-error')
      $('.companyId').removeClass('has-error')
      $('.email').removeClass('has-error')

    invalid: (view, attr, error)->
      if attr == 'company'
        $('.company').addClass('has-error')
        $('.txt').text('Nevivestai duomenys!')
        $('.alert').removeClass('alert-success')
        $('.alert').removeClass('alert-warning')
        $('.alert').addClass('alert-danger').show()
      if attr == 'city'
        $('.city').addClass('has-error')
        $('.txt').text('Nevivestai duomenys!')
        $('.alert').removeClass('alert-success')
        $('.alert').removeClass('alert-warning')
        $('.alert').addClass('alert-danger').show()
      if attr == 'address'
        $('.address').addClass('has-error')
        $('.txt').text('Nevivestai duomenys!')
        $('.alert').removeClass('alert-success')
        $('.alert').removeClass('alert-warning')
        $('.alert').addClass('alert-danger').show()
      if attr == 'companyId'
        $('.companyId').addClass('has-error')
        $('.txt').text('Nevivestai duomenys!')
        $('.alert').removeClass('alert-success')
        $('.alert').removeClass('alert-warning')
        $('.alert').addClass('alert-danger').show()
      if attr == 'email'
        $('.email').addClass('has-error')
        $('.txt').text('Blogas elektroninis pašto adresas!')
        $('.alert').removeClass('alert-success')
        $('.alert').removeClass('alert-warning')
        $('.alert').addClass('alert-danger').show()
     }
   )

  saveClient: ->
   @model.save(
    {
     company   : $('.company').val()
     country   : $('.country').val()
     city      : $('.city').val()
     address   : $('.address').val()
     zipCode   : $('.zipCode').val()
     companyId : $('.companyId').val()
     vat       : $('.vat').val()
     account   : $('.account').val()
     bank      : $('.bank').val()
     swift     : $('.swift').val()
     email     : $('.email').val()
     phone     : $('.phone').val()
     fax       : $('.fax').val()
    },
      error: (model, response) =>
       (@$ '.txt').text('Išsųsti nepavyko')
       (@$ '.alert').removeClass('alert-danger')
       (@$ '.alert').removeClass('alert-success')
       (@$ '.alert').addClass('alert-warning')
      success: (model, response) =>
       if response.clientInfo == 0
        (@$ '.txt').text('Sėkmingai išaugota!')
        (@$ '.alert').removeClass('alert-danger')
        (@$ '.alert').removeClass('alert-warning')
        (@$ '.alert').addClass('alert-success').show()
        @model.id = response.id
        vent.trigger 'addNewClient', @model
        $('.saveClient input').val ''
        @model.clear()
       else
        (@$ '.txt').text('Išsaugoti nepavyko')
        (@$ '.alert').removeClass('alert-success')
        (@$ '.alert').removeClass('alert-warning')
        (@$ '.alert').addClass('alert-danger').show()
   )

This is the template

<tbody class="clientList">
            <script type="text/template" id="Client-String">
                <td><%- company %></td>
                <td><%- companyId %></td>
                <td><%- phone %></td>
                <td><%- email %></td>
                <td><button class="btn btn-default edit" data-toggle="modal" data-t            arget="#editClient">{% trans %}Edit{% endtrans %}</button></td>
                <td><button class="btn btn-danger remove">{% trans %}Remove{% endtrans %}</button></td>
            </script>
            </tbody>

and the view for that template

class Info.View.Clients extends Backbone.View
  tagName: 'tr'

  template: template('Client-String')

  events:
    'click .remove': 'destroy'
    'click .edit': 'edit'

  initialize: ()->
    @model.on 'change', @render, @
    @render()

  render: ->
    @.$el.html @template @model.toJSON()

  edit: ->
    vent.trigger 'clientEdit', @model

  destroy: ->
    @model.destroy()
    @remove()

  remove: ->
    @.$el.remove()



 class Info.View.ClientList extends Backbone.View
   el: '.clientList'

   initialize: ()->
    model = new Info.Model.Client
    new Info.View.Client model: model
    vent.on 'addNewClient', @appendNewClient, @
    vent.on 'clientEdit', @getClientInfo, @
    @render()

   render: ->
     if @collection
     @collection.each(@appendNewClient, @)

   appendNewClient: (model)->
     @.$el.append(new Info.View.Clients(model: model).el)

   getClientInfo: (model)->
     $('.editForm').html(new Info.View.Edit(model: model).el)
1

1 Answers

0
votes

First change this line

success: (model, response) =>

to this one

success: (model, response) ->

and try to replace @model.clear() by model.clear() in order to clear the model passed to the success callback