Backbone.js $el method which is used to specify cached Query object for a view?s element by using which method user no need to re-warp the DOM element all the time.
[code]
view.$el
[/code]
el
By using Backbone.js el method user can define the element which is used as the view reference. this.el can be created by using the view's tagName, id, className and attribute properties, if specified. if not, el is an empty div. The code below demonstrats the Backbone.js el.
[code]
view.el
[/code]
Parameters
- properties
Which specify Instance properties for view class.
- classProperties
Which specify some class properties attached to the views constructor function.
The code below demonstrates the Backbone.js el().
[html]
<!DOCTYPE html>
<head>
<title>View el Example</title>
<script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js" type="text/javascript"></script>
</head>
<script type="text/javascript">
ViewDemo = Backbone.View.extend({
initialize: function(){
document.write("el property is defined..");
}
});
var viewdemo = new ViewDemo({ el: $("myapp") });
</script>
<body>
<div id="myapp"></div>
</body>
</html>
[/html]
Result
By running the above code in a preferred browser user can get the following output as shown in below image.