Backbone.js 了解主干中的el
我不太明白el在主干中是如何工作的 我假设el在未指定时默认为body。我编造了一把小提琴来说明我的误解 当我指定el时,一切正常。尽管未指定,但不会返回任何结果 HTML:Backbone.js 了解主干中的el,backbone.js,Backbone.js,我不太明白el在主干中是如何工作的 我假设el在未指定时默认为body。我编造了一把小提琴来说明我的误解 当我指定el时,一切正常。尽管未指定,但不会返回任何结果 HTML: 如果不在主干视图中指定元素,它将在内存中创建一个html节点,将视图呈现到其中,并基于该节点绑定所有事件处理程序。然后需要手动将其附加到dom中,如下所示: $('body').append(this.indexView.render().el); 基本上,你认为它默认为body是错误的。它将创建一个分离视图。您可能应
如果不在主干视图中指定元素,它将在内存中创建一个html节点,将视图呈现到其中,并基于该节点绑定所有事件处理程序。然后需要手动将其附加到dom中,如下所示:
$('body').append(this.indexView.render().el);
基本上,你认为它默认为body是错误的。它将创建一个分离视图。您可能应该查看,但如果未指定,则基本上,
el
是一个空div(未附加到DOM
)。
app = {};
app.Router = Backbone.Router.extend({
routes: {
"" : "index"
},
index: function() {
if (!this.indexView) {
this.indexView = new app.IndexView();
this.indexView.render();
} else {
this.indexView.refresh();
}
}
});
app.IndexView = Backbone.View.extend({
// el: $('.bar'),
template : _.template( $('#indexTemplate').html() ),
render: function() {
this.$el.html(this.template());
return this;
},
refresh: function() {
console.log('we\'ve already been here hombre.')
}
});
var router = new app.Router();
Backbone.history.start();
$('body').append(this.indexView.render().el);