【发布时间】:2011-11-30 18:08:16
【问题描述】:
我向模型添加了验证,而集合不会获取无效的模型。 (顺便说一句,我使用的是咖啡脚本,所以示例在咖啡脚本中)
有人知道解决方案吗?以下不工作
collection = new UserCollection
collection.fetch({
silent: true
success: ->
console.log('collection.models:', collection.models)
})
更新 1
我有很多没有手机号码的用户。
用户收藏:
class UserCollection extends Backbone.Collection
url: ->
app.routes.users_url
用户模型:
class User extends Backbone.Model
idAttribute: '_id'
defaults: {
"email": null
"mobile": null
"loc": null
}
url: ->
app.routes.users_url + '/' + (@id || '')
validate: (attrs) ->
if !attrs.email
return "Email address must be provided"
if !attrs.name
return "Name must be provided"
if !attrs.mobile
return "Mobile number must be provided"
if @isNew() and attrs.password != undefined
if !attrs.password
return "Password must be provided"
if attrs.password != attrs.password_confirmation
return "Passwords do not match"
model: User
更新 2
好的,我通过破解backbone.js 临时修复了它。
这发生在函数_prepareModel中
我改变了这一行:
if (model.validate && !model._performValidation(attrs, options)) model = false;
进入这一行:
if (!options.silent && model.validate && !model._performValidation(attrs, options)) model = false;
这不是一个解决方案,所以我保持这个问题开放
【问题讨论】:
-
您是否也尝试过传递
error回调?它会被调用吗? -
你能发布你的模型和验证码吗?
-
Odd... 如果您传入
silent: true标志,Collections.fetch 函数将不会运行您的验证器。你确定你的validate函数被调用了吗? documentcloud.github.com/backbone/docs/backbone.html#section-30 -
@BrianGenisio 请检查我的更新 2。该函数在 _prepareModel 中调用。即使你通过了沉默:真实。这可能是一个错误吗?还是我做错了什么?也许为此在github上打开一个问题是件好事
标签: javascript ruby-on-rails-3 backbone.js coffeescript