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

model.set: validate only when silent = false, why? #870

Closed
wolfgang-adamec opened this issue Jan 18, 2012 · 2 comments
Closed

model.set: validate only when silent = false, why? #870

wolfgang-adamec opened this issue Jan 18, 2012 · 2 comments
Labels

Comments

@wolfgang-adamec
Copy link

Hello!
With model.set I want to change my model, but I do not want events triggered, so I call "model.set" with silent=true.
But with this the validate function is not called.
Why?
Thanks alot
Wolfgang

current Backbone.js source code (0.5.3)
( set : function(key, value, options) {
...
// Run validation.
if (!options.silent && this.validate && !this._performValidation(attrs, options)) return false;)

@jashkenas
Copy link
Owner

Because the point of silent changes is that you're allowed to twiddle with the internal state of your model, temporarily, without actually making a change. Later, when the attribute actually changes, the validation runs and events are emitted. It wouldn't make sense to emit an error event when making a silent change.

@gudnm
Copy link

gudnm commented Jun 5, 2013

Jeremy, you are right, however this behavior makes possible to initialize an object with invalid parameter, which later throws off validate() when making valid changes. See http://stackoverflow.com/questions/8915288/backbone-model-validation or an example below -

> var me = new Person({'name': 'Ivan Vashchenko', age: -29});
undefined
> me.toJSON();
Object {name: "Ivan Vashchenko", age: -29, occupation: "farmer"}
> me.on('invalid', function(model, error) {
>   console.log(error);
> });
child {cid: "c2", attributes: Object, _changing: false, _previousAttributes: Object, changed: Object…}  
> me.save('name', 'My alter ego');
Person's age can not be negative.
false

It seems to me a validation could be useful on initialization as well...

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

No branches or pull requests

3 participants