根据有关验证的主干文档,它指出:
If validate returns an error, set and save will not continue, and the model attributes will not be modified.
因此,如果验证失败,我读取该设置或保存的方式不应该运行。但这不是我得到的结果。即使验证失败,它仍然会发送 POST/PUT 请求。我是在阅读文档错误还是在代码中做错了什么?
这是我的相关代码:
https://gist.github.com/80f6ef0099fbe96025dc
App.Models.Test = Backbone.Model.extend(
urlRoot: '/api/test'
validate: (attrs) ->
errors = []
if attrs.to is ''
errors.push
name: "to"
field: "js-to"
message: "You must enter a to address"
if attrs.subject is ''
errors.push
name: "subject"
field: "js-subject"
message: "You must enter a subject"
# Return our errors array if it isn't empty
errors if errors.length > 0
)
App.Views.Details = Backbone.View.extend(
initialize: ->
@model.bind "error", @error, this
events:
"click #js-save": "saveItem"
saveItem: (e) ->
e.preventDefault()
# Set the model then save it.
@model.set
subject: $("#js-subject").val()
message: $("#js-message").val()
mailbox_id: $("#js-from").val()
to: $("#js-to").val()
cc: $("#js-cc").val()
bcc: $("#js-bcc").val()
tags: App.Helpers.tagsToObject $('#js-tags').val()
scope: $('#js-scope').val()
attachments: attachments
@model.save null,
success: (model, response) =>
App.Helpers.showAlert "Success!", "Saved Successfully", "alert-success"
@next()
error: (model, response) ->
App.Helpers.showAlert "Error", "An error occurred while trying to save this item", "alert-error"
# Show the errors based on validation failure.
error: (model, error) ->
App.Helpers.displayValidationErrors error
最佳答案
你这样做是为了保存你的模型:
@model.save null,
success: -> ...
error: -> ...
那
null
是您麻烦的根源,请使用{}
事情会开始变得更好;如果你结合你的@model.set
和 @model.save
打电话,事情会更好:attrs =
subject: $("#js-subject").val()
#...
@model.save attrs,
success: -> ...
error: -> ...
一个
save
调用看起来像这样:save
model.save([attributes], [options])
[...]
The attributes hash (as inset
) should contain the attributes you'd like to change
所以通过
null
对于 attributes
表示您要按原样保存模型。当你
save
一个模型,验证主要留给 set
, code looks like this :if (attrs && !this.set(attrs, options.wait ? silentOptions : options)) {
return false;
}
您的
attrs
将是 null
所以set
不会被调用;但是,如果您让 save
处理您的set
,你会得到你所追求的行为。如果您通过了wait: true
选项,save
会manually run the validation关于传递的属性:if (options.wait) {
if (!this._validate(attrs, options)) return false;
...
}
内部
_validate
方法是 validate
的包装器做一些簿记和错误处理。你没有使用 wait: true
所以这不适用于你,但我认为无论如何都值得一提。考虑一个简单的示例,其模型
validate
总是失败。如果你这样说:@model.on 'error', @error
@model.save attrs,
success: -> console.log 'AJAX success'
error: -> console.log 'AJAX error'
然后
@error
将被调用,因为 save
最终会调用set
与 some attributes和 set
将调用 validate
.演示:http://jsfiddle.net/ambiguous/HHQ2N/1/但是,如果你说:
@model.save null, ...
null
will cause the set
call to be skipped .演示:http://jsfiddle.net/ambiguous/6pX2e/ (这里的 AJAX 会失败)。您的
@model.set
在 @model.save
之前调用应该触发您的错误处理程序,但如果您不检查 @model.set
返回,执行将盲目地继续到 save
调用并与您的服务器交谈。总之,这里发生了三件事:
save
他们就是你应该的样子。 @model.set
返回值并失去捕获验证错误的机会。 save(null, ...)
的参数处理可能会更好,但我不知道是否值得努力处理一种奇怪的调用方式。 你应该结合你的
set
/save
配对成一个 save
或查看set
返回。
关于validation - 验证失败时,Backbone Validate 正在发送请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10983033/