Backbone.js 集合不会获取无效的模型

发布于 2024-12-08 01:19:21 字数 1482 浏览 0 评论 0原文

我向模型添加了验证,而集合不会获取无效的模型。 (顺便说一句,我使用咖啡脚本,所以示例在咖啡脚本中)

有人知道这个问题的解决方案吗?以下不起作用

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无畏 2024-12-15 01:19:21
"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)"

如果您的模型未验证,则说明您的模型或验证存在问题。

"I have a lot of users without a mobile number."

在验证中,您可以:

if !attrs.mobile
  return "Mobile number must be provided"

您可以在集合中定义一个解析函数来记录来自服务器的模型(parse() 传递来自 fetch() 的原始响应)

parse: function(response) {
  console.log(response.results);
  return response.results;
}

,或者您可以采用验证是否存在的行手机号码无法验证,因为您不知道用户是否有手机号码。

为了涵盖所有基础,为 fetch() 定义一个错误函数应该可以帮助您:

collection.fetch({
  silent: true
  success: ->
    console.log('collection.models:', collection.models)
  error: (collection, response) ->
    console.log('response')
})
"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)"

If your models don't validate you have a problem with your models or your validation.

"I have a lot of users without a mobile number."

In your validation you have:

if !attrs.mobile
  return "Mobile number must be provided"

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())

parse: function(response) {
  console.log(response.results);
  return response.results;
}

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:

collection.fetch({
  silent: true
  success: ->
    console.log('collection.models:', collection.models)
  error: (collection, response) ->
    console.log('response')
})
心奴独伤 2024-12-15 01:19:21

验证模型时,请检查 model.silent 并仅在其不存在时进行验证。

因此,当您想要获取模型时,请执行以下操作:

var test = new MyModel({ id: '123', silent: true }); 

// in your Model validate function
validate: function(attrs) {
  if (!attrs.silent) {
    // validate logic here
  }
}

然后您可以获取模型。获得模型后,您可以取消静音。

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:

var test = new MyModel({ id: '123', silent: true }); 

// in your Model validate function
validate: function(attrs) {
  if (!attrs.silent) {
    // validate logic here
  }
}

Then you can fetch the model. After you get your model you can unset silent.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文