Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse model before validating on model.set(pojo, {parse:true}) #3757

Closed
maxponte opened this issue Aug 13, 2015 · 1 comment
Closed

Parse model before validating on model.set(pojo, {parse:true}) #3757

maxponte opened this issue Aug 13, 2015 · 1 comment

Comments

@maxponte
Copy link

If I have a Backbone model that expects a wrapped object from the API e.g.
{a: {a: 3}}

But I want to work with it as
{a:3}

It is nice to use parse like

parse: function(data) {
  return data.a;
}

And validate like

validate: function(attrs) {
  if(!_.has(attrs, 'a') || !_.isNumber(attrs.a)) {
    return 'incorrect signature';
  }
}

And when I mock out the model in a test I do:

model.set({a: {a:3}}, {parse: true})

But this fails because model.set calls validate before parsing my object. This doesn't make a whole lot of sense to me. If I say {parse: true} it should parse before validating. My workaround is

var o = {a: {a:3}};
model.set(model.parse(o));

But I feel like it would be less confusing to have {parse: true} actually work as expected when you have a validate function.

@jridgewell
Copy link
Collaborator

Model#set doesn't have a parse option. I think you're thinking of Colleciton#set which (sadly) does, but that'll be gone in v2.

See #3644's OP for reasoning.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants