好的,我正在尝试将客户端表单保存到服务器。我正在使用boostrap模式来存储表单和下划线模板以从主干获取for。在提交表单之后,表单会很好并且表单会转到后端并保存在数据库中。我遇到的问题是,在我保存客户端并希望为另一个客户端输入新信息之后,它发送了一个PUT方法,我使用“@ model.clear()”解决了这个问题。现在每件事情仍然有效但我在控制台中收到“未捕获的ReferenceError:公司未定义”错误。我知道这是因为“.clear()”虽然我不知道如何解决它。
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()
)
这是模板
<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>
以及该模板的视图
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)
答案 0 :(得分:0)
首先更改此行
success: (model, response) =>
到这个
success: (model, response) ->
并尝试将@model.clear()
替换为model.clear()
,以便清除传递给model
回调的success