Backbone.js 集合不会获取无效的模型
我向模型添加了验证,而集合不会获取无效的模型。 (顺便说一句,我使用咖啡脚本,所以示例在咖啡脚本中)
有人知道这个问题的解决方案吗?以下不起作用
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;
这不是一个解决方案,所以我保持这个问题开放
I added a validation to a Model and a Collection wont fetch the models who arent valid. (Btw I use coffeescript so the examples are in coffeescript)
Somebody knows a solution for this? The following isnt working
collection = new UserCollection
collection.fetch({
silent: true
success: ->
console.log('collection.models:', collection.models)
})
UPDATE 1
I have a lot of users without a mobile number.
User Collection:
class UserCollection extends Backbone.Collection
url: ->
app.routes.users_url
User Model:
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
UPDATE 2
ok i temporary fixed it by hacking the backbone.js.
It is happening in function _prepareModel
I changed this line:
if (model.validate && !model._performValidation(attrs, options)) model = false;
into this line:
if (!options.silent && model.validate && !model._performValidation(attrs, options)) model = false;
It is not a solution so i keep this question open
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的模型未验证,则说明您的模型或验证存在问题。
在验证中,您可以:
您可以在集合中定义一个解析函数来记录来自服务器的模型(parse() 传递来自 fetch() 的原始响应)
,或者您可以采用验证是否存在的行手机号码无法验证,因为您不知道用户是否有手机号码。
为了涵盖所有基础,为 fetch() 定义一个错误函数应该可以帮助您:
If your models don't validate you have a problem with your models or your validation.
In your validation you have:
you could define a parse function in your collection to log what models are coming from the server (parse() gets passed the raw response from fetch())
or you can take the line that validates the existence of a mobile number out of your validation since you don't know if the user has a mobile number.
and just to cover all the bases, defining an error function for fetch() should help you:
验证模型时,请检查
model.silent
并仅在其不存在时进行验证。因此,当您想要获取模型时,请执行以下操作:
然后您可以获取模型。获得模型后,您可以取消静音。
When you validate your model, check for
model.silent
and only validate if that doesn't exist.So you do the following when you want to fetch a model:
Then you can fetch the model. After you get your model you can unset silent.