因此,基本上this.template
将(在大多数情况下)html template
的编译版本,你认为。
它将在其中占位符,并将参数与模板中占位符相同的键。例如(把手模板),
<section id="{{id}}">
<header>{{header_text}}</header>
</section>
考虑到上述代码作为模板,在编译时,并将其存储在this.template
,它返回一个函数,它接受一个json
对象作为参数,所以现在this.template
是一个功能。
你可以这样调用它下面,
var html_text = this.template({
id : "main_content",
header_text : "Hi Welcome !!"
});
this.$el.html(html_text);
执行后,el
的内容将是
<section id="main_content">
<header>Hi Welcome !!</header>
</section>
所以当你做this.$el.html(this.template(this.model.toJSON());
,它实际上产生了所需的json
参数this.template
方法适合你,因此工作正常。
而且如Loamhoof所述,在this.$el.html(this.model.get("city"));
中,您将使用html方法,该方法将根据模型的属性值设置el的html内容。