Backbone.js view constructor is first created when the view is first made. It initializes the view. When a view is generated, it is called and uses the new keyword. It is one of a number of unique options that will be directly linked to the view. If initialize method is declared and defined inside the view, it is automatically called when a view object is created, we can call all the other functions inside this method using this keyword which will terminate the need to call them with the object reference later.
new View( options )
- options: This parameter is optional. It can be a lot of different values like a model, collection, id, className, tagName, attributes, and events.
Example 1: The code below shows how we can create a simple initialize function in a view.
Example 2: The code below shows how we can manipulate HTML by using the el property. Then we need to add the content we want to add/change to the $el jQuery object. Also, we haven’t called the render function with the object reference, it is called upon by the initialize function.
Please Login to comment...