Monday, 11 June 2018

011 BackboneJS Model destroy,validate,isValid,url

destroy :Destroys the model on the server by using the 
Backbone.sync.

Syntax :model.destroy()




Example


<!DOCTYPE html>
<html>
<head>

          <script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>

           <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js" type="text/javascript"></script>

           <script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js" type="text/javascript"></script>


<script type="text/javascript">

               Backbone.sync = function (method, model) 
        {
                  document.write(method + ": " + JSON.stringify(model) + '');
                  model.set('id', 1);
              };


            var student = new Backbone.Model
             ({
                  name: "john",
                  class: 4
             });

          student.save();
          student.destroy();

</script>
</head>
</html>

Output :

create: {"name":"john","class":4}
delete: {"name":"john","class":4,"id":1}


validate :This method is undefined and you can write your custom validation logic with the help of JavaScript

Syntax : model.validate(attributes, options)

Example

<!DOCTYPE html>
<html>
<head>

<script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js"
type="text/javascript"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js"
type="text/javascript"></script>


<script type="text/javascript">


var student = Backbone.Model.extend({
                          defaults: {
                                               name: 'john',
                                               age: 2
                                           },
 
validate: function (attrs) 
{

              if (attrs.name) 
        {
                 if (attrs.name.length < 5) 
          {
                      document.write('Please enter your full name.');
                  }
              }

             if (attrs.age) 
       {
                if (attrs.age < 18) 
          {
                   document.write('You are less than 18.');
                 }
            }

     }
});

var stuObj = new student();

stuObj.set({ age: '2' }, { validate: true });


</script>
</head>
</html>

Output :

Please enter your full name.
You are less than 18.


isValid :Validation check for model state.
Syntax : model.isValid()

Example

<!DOCTYPE html>
<html>
<head>

<script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js"
type="text/javascript"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js"
type="text/javascript"></script>


<script type="text/javascript">


var student = Backbone.Model.extend({
                        defaults: {
                        name: 'john',
                        age: 2
                                        },

validate: function (attrs) 
{

       if (attrs.name) 
       {
             if (attrs.name.length < 5) 
             {
                           document.write('Please enter your full name.');
                      }
          }

 
     if (attrs.age) 
     {
             if (attrs.age < 18) 
        {
                  document.write('You are less than 18.');
             }
       }

  }
});

var stuObj = new student();

stuObj.set({ name:'John2',age: '19'}, { validate: true });

if (stuObj.isValid())
{
        document.write('You model is valid.');
}
</script>
</head>
</html>

Output :
You model is valid.
 

url : Returns the relative URL where the model's resource is located.

Syntax :model.url()

Example

<!DOCTYPE html>
<html>
<head>

<script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js"
type="text/javascript"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js"
type="text/javascript"></script>


<script type="text/javascript">


var student = Backbone.Model.extend({
urlRoot: '/tutorialspoint/backbonejs'
});

var stuObj = new student();

document.write(stuObj.url());
</script>
</head>
</html>

Output :
 /tutorialspoint/backbonejs




 








 
 

No comments:

Post a Comment

বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...