Home > Web Front-end > JS Tutorial > body text

How to create a simple view using Backbone.js

不言
Release: 2018-11-30 17:37:30
Original
2487 people have browsed it

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.

<!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;
      $(&#39;#output&#39;).append(mView.el);
    });  
</script>
</head>
<body>
  <div>Header</div>
  <div id="output"></div>
  <div>Footer</div>
</body>
</html>
Copy after login

Explanation:

 var CView = Backbone.View.extend({
  });
Copy after login

The above code becomes the object of the view. In this code, nothing is implemented.

 var mView = new CView;
  $(&#39;#output&#39;).append(mView.el);
Copy after login

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.

<!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(&#39;Hello World!&#39;);
          return this;
        }
      });
      var mView = new CView;
      $(&#39;#output&#39;).append(mView.render().el);
    });
  </script>
</head>
<body>
  <div>Header</div>
  <div id="output"></div>
  <div>Footer</div>
</body>
</html>
Copy after login

Reference: It also works with the following code.

$(function () {
      var CView = Backbone.View.extend({
              render: function () {
                        this.$el.text(&#39;Hello World!&#39;);          
                        return this;
        }
      });
      var mView = new CView;
      mView.render();
      $(&#39;#output&#39;).append(mView.el);
    });
Copy after login

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(&#39;Hello World!&#39;);      
    return this;
    }
  });
Copy after login

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;
  $(&#39;#output&#39;).append(mView.render().el);
Copy after login

Or,

var mView = new CView;
  mView.render();
  $(&#39;#output&#39;).append(mView.el);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template