El modelo de obtención de Backbone.js se usa para devolver un valor de un atributo particular del modelo.
Sintaxis :
Backbone.Model.get(attribute)
Parámetro: Acepta un único parámetro:
- atributo: Se utiliza para definir el atributo para el modelo creado.
Ejemplo 1: En este ejemplo, devolveremos los atributos del modelo de libro.
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>
Producción:
Values: {"bookid":23,"price":678,"book_name":"css"} bookid: 23 price: 678 book_name: css
Ejemplo 2: En este ejemplo, modificaremos el valor y obtendremos los valores.
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>
Salida :
Values: {"bookid":23,"price":678,"book_name":"css"} book_name: php
Referencia: https://backbonejs.org/#Model-get
Publicación traducida automáticamente
Artículo escrito por sireeshakanneganti112 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA