The Backbone.js attributes model is used to define the property of the given model and uses the update/change the attributes using set() method.
Syntax:
Backbone.Model.attributes
Parameters: It does not accept any parameter.
Example 1: In this example, we will set the book with 1 attribute using the set() method and return 1 attribute using the get() method. The set() method performs a smart update of the collection with a set of items in the model while get() method is used to retrieve a model from a collection.
HTML
<!DOCTYPE html> <html> Â Â <head> Â Â Â Â Â Â Â Â Â Â 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(); Â Â Â Â Â Â Â Â book.set({ bookid: 23 }); Â Â Â Â Â Â Â Â document.write('bookid: ', book.get('bookid')); Â Â Â Â </script> </body> Â Â </html> |
Output:
bookid: 23
Example 2: In this example, we will set the book with 3 attributes using the set() method and return 3 attributes one by one using the get() method.
HTML
<!DOCTYPE html> <html> Â Â <head> Â Â Â Â Â Â Â Â 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(); Â Â Â Â Â Â Â Â book.set({ bookid: 23, price: 678, book_name: "css" }); Â Â Â Â Â Â Â Â document.write('bookid: ', book.get('bookid')); Â Â Â Â Â Â Â Â document.write("<br>"); Â Â Â Â Â Â Â Â document.write('price: ', book.get('price')); Â Â Â Â Â Â Â Â document.write("<br>"); Â Â Â Â Â Â Â Â document.write('book_name: ', book.get('book_name'));Â Â Â Â Â Â </script> </body> Â Â </html> |
Output:
bookid: 23 price: 678 book_name: css
Reference: https://backbonejs.org/#Model-attributes
