How to create a simple view using How to create a simple view using Backbone.js? This article introduces you to how to use How to create a simple view using Backbone.js to create a simple view. Let’s take a look at the specific content.
Example 1:
The code is as follows
Create the following HTML file.
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <script src="jquery-3.2.1.js"></script> <script src="underscore.js"></script> <script src="backbone.js"></script> <script type="text/javascript"> $(function () { var CView = Backbone.View.extend({ }); var mView = new CView; $('#output').append(mView.el); }); </script> </head> <body> <div>Header</div> <div id="output"></div> <div>Footer</div> </body> </html>
Explanation:
var CView = Backbone.View.extend({ });
The above code becomes the object of the view. In this code, nothing is implemented.
var mView = new CView; $('#output').append(mView.el);
We create an object of the view and add the el attribute of the view object to the tag element that outputs the id.
Run results
When the HTML file is displayed in the web browser, the following effect will be displayed. Nothing seems to be displayed.
When confirming the output of the HTML, you can see that the div tag is added inside the div tag with id="output". Although the string is not displayed on the page, you can confirm that the view elements of the view are reflected on the page.
Example 2: String display view
View is implemented to be able to display strings.
The code is as follows
Write the following HTML file.
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <script src="jquery-3.2.1.js"></script> <script src="underscore.js"></script> <script src="backbone.js"></script> <script type="text/javascript"> $(function () { var CView = Backbone.View.extend({ render: function () { this.$el.text('Hello World!'); return this; } }); var mView = new CView; $('#output').append(mView.render().el); }); </script> </head> <body> <div>Header</div> <div id="output"></div> <div>Footer</div> </body> </html>
Reference: It also works with the following code.
$(function () { var CView = Backbone.View.extend({ render: function () { this.$el.text('Hello World!'); return this; } }); var mView = new CView; mView.render(); $('#output').append(mView.el); });
Description:
Describe the rendering implementation in the render method. The markup to be output to HTML is set to el object. The jquery object of el can be used as $el. Since this code uses jquery, we use $el instead of el. Call the text method and set the string "Hello World!" to the el object.
var CView = Backbone.View.extend({ render: function () { this.$el.text('Hello World!'); return this; } });
Create an object for the view. Call the render() method for page display. By executing the render method, the string "Hello World!" is set to the el attribute, and the page display content can be prepared.
Then, it displays the content of the el attribute in an element whose value is id. The el property can refer to the el property of the View object, and the same result can be obtained by referencing the el property of the View object returned as the return value of the render() method.
var mView = new CView; $('#output').append(mView.render().el);
Or,
var mView = new CView; mView.render(); $('#output').append(mView.el);
Running results
Use a web browser to display the above HTML file. The effect shown below will be displayed. The string "Hello World!" is displayed on the page.
When you check the output of the HTML, you can see that the div tag is added inside the div tag with id="output" and the string "Hello World!" Write in it.
The above is the detailed content of How to create a simple view using Backbone.js. For more information, please follow other related articles on the PHP Chinese website!