目前我正在对 Backbone.js 进行一些研究,我想知道为什么$el
在创建我的listItemView
.
// autoCompleteView.js
/**
* Create a new instance of {listItem} and
* append it to result list.
*
* @param item
* @see listItem
* @private
*/
_addResultListItem: function (item) {
this.$el.append(
new this.listItem({
model: item,
parent: this
}).render().$el
);
}
更新:
渲染方法
// autoCompleteView.js
render: function () {
// bind events
this.input
.keyup(this.keyup.bind(this))
.keydown(this.keydown.bind(this))
.after(this.$el);
return this;
}
项目视图
// autoCompleteItemView.js
var AutoCompleteItemView = Backbone.View.extend({
tagName: "li",
events: {
"click": "select"
},
render: function () {
this.$el.append( Mustache.to_html( Template, this.model.toJSON() ) );
return this;
},
select: function () {
this.options.parent.hide().select(this.model);
return false;
}
});
return AutoCompleteItemView;