Backbone.js collection wont fetch models that arent valid

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

Answers


"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')
})

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.


Need Your Help

Should a Python generator raise an exception when there are no more elements to yield?

python

Should a Python generator raise an exception when there are no more elements to yield?

how to convert canvas to image to upload to flask?

javascript python ajax canvas flask

Alright so I'm trying to upload a resized canvas image as a file to Flask.