Monday, 11 June 2018

009 BackboneJS Model has,unset,clear,id,IdAttribute,Cid

has : Returns "true if the attribute is non-null or non-undefined value.

Syntax : model.has(attribute)

if (model.has("attribute name")) {

}


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">

student = Backbone.Model.extend({
                   defaults:
                       {
                                       name: "",
                                       class: ""
                        },
                   initialize: function (a, b)
                    {
                                       this.set("name", a);
                                      this.set("class", b);
                    }
              });
var mymodel = new student('john', 12);

                if (mymodel.has('name')) 
         {
                                 document.write("The model student has name: True");
               
           else 
         {
                                 document.write("The model student has name: False");
                }

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

Output :

The model student has name: True  

unset :It is used to remove an attribute from a backbone model.

Syntax:model.unset(attribute)

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">

student = Backbone.Model.extend({
               defaults:
               {
                   name: "",
                   class: ""
              },
              initialize: function (a, b)
              {
               this.set("name", a);
               this.set("class", b);
             }
});

var mymodel = new student('john', 12);

mymodel.unset('name');

document.write("
"
);
document.write("After unset, student name is:",
mymodel.get('name'));

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

Output :
After unset, student name is:undefined


clear : Removes all attributes of a model.


Syntax:model.clear([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">

student = Backbone.Model.extend({
                  defaults:
                {
                         name: "",
                         class: ""
                 },
                  initialize: function (a, b)
                 {
                      this.set("name", a);
                      this.set("class", b);
                  }
             });

      var mymodel = new student('john', 12);


      mymodel.clear();

      document.write("
"
);
      document.write("After unset, student name is:",
       mymodel.get('name'));

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

Output :

After unset, student name is:undefined 

id : The id is an arbitrary string (integer id or UUID),It uniquely identifies the model entity.set when model is created.  

Syntax:model.id
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">

student = Backbone.Model.extend({
                 defaults:
                 {
                            id: 10
                  }
                });

var mymodel = new student();


document.write("
"
);
document.write("After unset, student id is:",
mymodel.get('id'));

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

Output :

After unset, student id is:10

IdAttribute :It gives you model's unique identifier that contains the name of the member of the class, that is use as id.

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">

student = Backbone.Model.extend({
                 defaults:
                 {
                           idAttribute: 'id',
                           name :'',
                           age :''
                }
});

var mymodel = new student({id:10,name:'john',age:'12'});

document.write("
"
);
document.write("Unique indentifier name is :", mymodel.idAttribute+'');
document.write("Unique indentifier value is :", mymodel.get(mymodel.idAttribute));

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

Output :

Unique indentifier name is :id
Unique indentifier value is :10


Cid : Cid specifies the client id which is automatically generated by Backbone.js. The Backbone.js Cid model can be uniquely identified on the client.

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">

student = Backbone.Model.extend({
defaults:
           {
               idAttribute: 'id',
               name :'',
               age :''
          }
});

var mymodel = new student({ id: 10, name: 'john', age: '12' });

document.write("Unique indentifier name is :", mymodel.cid+'');

var mymodel1 = new student({ id: 11, name: 'john2', age: '15' });

document.write("Unique indentifier name is :", mymodel1.cid + '');

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

Output :

Unique indentifier name is :c1
Unique indentifier value is :c2

No comments:

Post a Comment

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

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