Home>Article>Web Front-end> How to create a simple view using Backbone.js

How to create a simple view using Backbone.js

不言
不言 Original
2018-11-30 17:37:30 2447browse

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.

How to create a simple view using Backbone.js

Example 1:

The code is as follows

Create the following HTML file.

         
Header
Footer

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.

How to create a simple view using Backbone.js

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.

         
Header
Footer

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.

How to create a simple view using Backbone.js

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn