This article mainly introduces the example of JavaScript Backbone. view view in the js framework. In the browser-side js framework backbone, we use View to bind and handle DOM events, you can refer to the View in Backbone to reflect the Model in your app. They listen to events and respond accordingly.
In the next tutorial, I will not tell you how to bind the Model and Collection to the View, but mainly discuss how the View uses the javascript template library, especially Underscore. js's _. template.
Here we use jQuery to operate DOM elements. Of course you can also use other libraries, such as MooTools or Sizzle. However, jQuery is recommended in official documents of Backbone.
Next, we will use the search box as an example to create a new View:
SearchView = Backbone.View.extend({ initialize: function(){ alert("Welcome to Backbone!"); }});var search_view = new SearchView();
Whether it is Model, View or Collection, the initialize () method is automatically triggered when it is instantiated.
El attributes
The el attribute refers to a DOM object that has been created in the browser. Each View has an el attribute. If it is not defined, backbone creates an empty p element as the el attribute.
Next let's create an el attribute for the View and set it to # search_containe.