Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Backbone.js 了解主干中的el_Backbone.js - Fatal编程技术网

Backbone.js 了解主干中的el

Backbone.js 了解主干中的el,backbone.js,Backbone.js,我不太明白el在主干中是如何工作的 我假设el在未指定时默认为body。我编造了一把小提琴来说明我的误解 当我指定el时,一切正常。尽管未指定,但不会返回任何结果 HTML: 如果不在主干视图中指定元素,它将在内存中创建一个html节点,将视图呈现到其中,并基于该节点绑定所有事件处理程序。然后需要手动将其附加到dom中,如下所示: $('body').append(this.indexView.render().el); 基本上,你认为它默认为body是错误的。它将创建一个分离视图。您可能应

我不太明白el在主干中是如何工作的

我假设el在未指定时默认为body。我编造了一把小提琴来说明我的误解

当我指定el时,一切正常。尽管未指定,但不会返回任何结果

HTML:


如果不在主干视图中指定元素,它将在内存中创建一个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);