In this article, we will see the Backbone.js has() model. The Backbone.js has() model is used to check if an attribute contains value or not. if the value exists or it’s a not-null value, it will return “true”, if it is null, it will return “false”.
Syntax:
Backbone.Model.has(attribute);
Parameters: It accepts one parameter:
- attribute: It specifies the attribute in a model.
Example 1: Check whether the model book attributes have values or not by using has() model.
HTML
<!DOCTYPE html> <html> <head> <script src= type="text/javascript"> </script> <script src= type="text/javascript"> </script> <script src= type="text/javascript"> </script> </head> <body> <script type="text/javascript"> var Books = Backbone.Model.extend(); var book = new Books({bookid:null,price:null,book_name:null}); document.write(" bookid is not-null? : ",book.has('bookid')); document.write("<br>"); document.write(" price is not-null? : ",book.has('price')); document.write("<br>"); document.write(" book_name is not-null? : ",book.has('book_name')); </script> </body> </html> |
Output:
bookid is not-null? : false price is not-null? : false book_name is not-null? : false
Example 2: It checks whether the model book has values or not.
HTML
<!DOCTYPE html> <html> <head> <script src= type="text/javascript"> </script> <script src= type="text/javascript"> </script> <script src= type="text/javascript"> </script> </head> <body> <script type="text/javascript"> var Books = Backbone.Model.extend(); var book = new Books({bookid:6,price:4555,book_name:'php'}); document.write(" bookid is not-null? : ",book.has('bookid')); document.write("<br>"); document.write(" price is not-null? : ",book.has('price')); document.write("<br>"); document.write(" book_name is not-null? : ",book.has('book_name')); </script> </body> </html> |
Output:
bookid is not-null? : true price is not-null? : true book_name is not-null? : true
Reference: https://backbonejs.org/#Model-has
