[英]Backbone model validation undefined attrs
So I've got my model which I'm trying to validate, according to the books this should work, however I'm receiving attrs as undefined. 因此,根据正在使用的书籍,我已经尝试验证模型,但是我收到的attrs未定义。
validate: (attrs, options)->
attrs
if I go ahead and do it this way: 如果我继续这样做:
validate: ->
this.attributes
I can access the model's attributes just fine however I don't think this is the recommended way to do it. 我可以很好地访问模型的属性,但是我认为这不是推荐的方法。
this is my model code: 这是我的模型代码:
class Todo extends Backbone.Model
defaults:
title: 'default title'
completed: false
validate: (attrs, options)->
attrs
myTodo new Todo
myTodo.validate()
//returns false because attrs is undefined
what am I missing? 我想念什么?
From the fine manual : 从精美的手册中 :
validate
model.validate(attributes, options)
验证
model.validate(attributes, options)
[...] By default validate is called before
save
, but can also be called beforeset
if{validate:true}
is passed.[...]默认情况下,在
save
之前调用validate ,但是如果传递了{validate:true}
,也可以在set
之前调用{validate:true}
。
So validate
is meant to be called by Backbone, not directly by you. 因此
validate
是由Backbone而不是您直接调用的。 You're calling validate
yourself: 您正在打电话
validate
自己:
myTodo.validate()
but not passing any arguments so attrs
is undefined
because, well, you didn't pass its value. 但未传递任何参数,因此
undefined
attrs
,因为您没有传递其值。
You're supposed to let Backbone call validate
when you call myTodo.save()
or myTodo.set(new_values, validate: true)
. 您应该在调用
myTodo.save()
或myTodo.set(new_values, validate: true)
时让Backbone呼叫validate
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.