Backbone.js get Model
The Backbone.js get model is used to return a value of a particular attribute from the model.
Syntax:
Backbone.Model.get(attribute)
Parameter: It accepts an only a single parameter:
- attribute: It is used to define the attribute for the created model.
Example 1: In this example, we will return the attributes of the book model.
HTML
<!DOCTYPE html> < html > < head > < title >Backbone.js get Model</ title > < 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 > </ head > < body > < script type = "text/javascript" > var Books = Backbone.Model.extend(); var book = new Books({ bookid: 23, price: 678, book_name: "css" }); document.write(' Values: ' + JSON.stringify(book)); document.write("< br >"); 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:
Values: {"bookid":23,"price":678,"book_name":"css"} bookid: 23 price: 678 book_name: css
Example 2: In this example, we will modify the value and get the values.
HTML
<!DOCTYPE html> < html > < head > < title >Backbone.js get Model</ title > < 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 > </ head > < body > < script type = "text/javascript" > var Books = Backbone.Model.extend(); var book = new Books({ bookid: 23, price: 678, book_name: "css" }); document.write(' Values: ' + JSON.stringify(book)); book.set({ bookid: 33, price: 211, book_name: "php" }); document.write("< br >"); document.write('book_name: ', book.get('book_name')); </ script > </ body > </ html > |
Output:
Values: {"bookid":23,"price":678,"book_name":"css"} book_name: php
Reference: https://backbonejs.org/#Model-get