this
和initialize
属性中render
引用的内容是什么?
JavaScript代码:
SearchView = Backbone.View.extend({
initialize: function(){
this.render();
},
render: function(){
// Compile the template using underscore
var template = _.template( $("#search_template").html(), {} );
// Load the compiled HTML into the Backbone "el"
this.$el.html( template );
}
});
var search_view = new SearchView({ el: $("#search_container") });
HTML code:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Backbone.js App</title>
<meta name="description" content="">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.3.3/backbone-min.js"></script>
</head>
<body>
<div id="search_container"></div>
<script type="text/template" id="search_template">
<label>Search</label>
<input type="text" id="search_input" />
<input type="button" id="search_button" value="Search" />
</script>
<script src="app.js"></script>
</body>
</html>
答案 0 :(得分:0)
在您的具体情况下,它是search_view
。
尝试以下方法:
console.log('this in initialize', this)
console.log('this in render', this)
console.log('search_view', search_view)
var search_view = new SearchView(...);
并了解这些结果是如何相同/不同的。