【问题标题】:Backbone.js collection wont fetch models that arent validBackbone.js 集合不会获取无效的模型
【发布时间】: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


【解决方案1】:
"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')
})

【讨论】:

  • 问题是,即使我添加了silent:true,集合也没有获取它们。
  • 值得注意的是,集合中的parse 方法不一定传递带有results 键的对象。这取决于您使用的 API。 Twitter API 传递一个带有 results 键的对象,但一个普通的 Backbone 应用程序只会传递一个模型数组。
【解决方案2】:

验证模型时,检查model.silent,仅在不存在时验证。

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

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

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

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

【讨论】:

    猜你喜欢
    • 2012-07-08
    • 2013-07-10
    • 1970-01-01
    • 2011-10-31
    • 2011-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多