Vinay Rajput
Vinay Rajput

Reputation: 93

is this a bug of backbonejs set function

Hi I am new to backbonejs,

I have encountered a problem while i was trying an example of http://addyosmani.github.com/backbone-fundamentals/#validation

I have created an 2 object of model, myTodo and myTodo1,

if I call set function like this, it return completed:false due to validation error


    var myTodo  = new Todo();
    myTodo.set('completed', true, {validate: true}); 
    console.log("completed: "+myTodo.get('completed'));
    /*
    The above code returns following log:
    This model has been initialized.
    Remember to set a title for your todo.
    completed: false
    */

buy why validation is not executing on execution of below code


    var myTodo1  = new Todo();
    myTodo1.set('completed',true);
    console.log("completed: "+myTodo1.get('completed'));
    myTodo1.set({validate:true})
    console.log("completed: "+myTodo1.get('completed'));
    /*
    The above code returns following log:
    This model has been initialized. underscore-test2.js:11
    completed: true underscore-test2.js:28
    completed: true 
    */

although both set of code doing the same job, but in first case validation executes but not in second case

Why?????

below is the full example code.


    var Todo = Backbone.Model.extend({
      defaults: {
        completed: false,
      },
      validate: function(attribs){
        if(attribs.title === undefined){
            return "Remember to set a title for your todo.";
        }
      },
      initialize: function(){
        console.log('This model has been initialized.');
        this.on("invalid", function(model, error){
            console.log(error);
        });
        this.on('change:title', function(){
            console.log('- Values for title have changed.');
        });
      }
    });

    **var myTodo  = new Todo();
    myTodo.set('completed', true, {validate: true}); 
    console.log("completed: "+myTodo.get('completed'));**

    **var myTodo1  = new Todo();
    myTodo1.set('completed',true);
    console.log("completed: "+myTodo1.get('completed'));
    myTodo1.set({validate:true})
    console.log("completed: "+myTodo1.get('completed'));**

Upvotes: 0

Views: 105

Answers (1)

Loamhoof
Loamhoof

Reputation: 8293

They're simply totally different.

myTodo1.set({validate:true})

Here you're not running the validation. You're simply setting an attribute "validate" to true for the model. So basically, in the second example, you're setting 2 attributes without validating.

In any case, you cannot hope to do the validation afterwards, as your attribute would have already been set.

Edit:
I don't know the behavior when setting a new value to the validate attribute though. But you're certainly overriding your validation function by a boolean. This would certainly lead to unexpected behaviors.

Edit 2:

Ok, I guess that'll be my last attempt.

myTodo1.set({completed:true});
myTodo1.set({validate:true});

also equivalent to:

myTodo1.set({completed: true}).set({validate: true});

BUT NOT EQUIVALENT, IN ANY CASE, TO:

myTodo1.set({completed: true}, {validate: true});

In the lattest, the {validate: true} tells Backbone to apply the validation function on the model.

In the former, you only set the completed attribute to true without telling the model to validate. THEN you set the validate attribute to true once again without telling the model to validate. So basically you never tell the model to validate.

Upvotes: 1

Related Questions