[英]Refreshing backbone form (including events) after validation error without reloading page
我有一個帶有表單的 Backbone 視圖。 當用戶提交表單時,可能會出現驗證錯誤,這是由后端處理的。 顯示錯誤后,我希望表單再次可用。 我以為我應該只做 this.delegateEvents(),但由於某種原因它不起作用......
這是代碼:
class App.Views.PlotsCreate extends Backbone.View
template: JST['plots/create']
events:
'submit #new_plot': 'createPlot'
createPlot: (event) ->
event.preventDefault()
@attributes = plot: {name: $('#new_plot_name').val(), docreate: "true", code: code }
@collection.create @attributes,
wait: true
success: (plot) =>
document.body.style.cursor = 'default'
@showProgress(plot)
error: @handleError
)
handleError: (entry, response) =>
if response.status == 422
errors = $.parseJSON(response.responseText).errors
for attribute, messages of errors
$('#new_plot_name').val("#{message}" for message in messages)
<form class="new_plot" name="create_form" id ="new_plot" data-remote="true" enctype="multipart/form-data">
<textarea class="input" id="new_plot_name" name="name" rows="5" maxlength = '140'></textarea>
<input class="blue_button btn_generate" name="commit" type="submit" value="create" id ="plot_subm"/>
</form>
如何使表單再次發揮作用(即在顯示錯誤后再次觸發提交事件)
驗證失敗通常不會對事件綁定做任何事情; 事實上,從技術上講,視圖甚至沒有驗證方法,只有模型有(並且該方法所做的只是觸發“錯誤”事件並返回 false)。 您確定您的末端沒有一些破壞視圖的代碼嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.