2014-11-04 117 views
1

我無法渲染Marionette LayoutView並在該佈局中顯示區域。Backbone Marionette LayoutView無法找到DOM元素

我的佈局文件:

template: '#fooTemplate', 
    regions: { 
     barRegion: '.bar' 
    } 

我的HTML:

<script id="fooTemplate" type="text/template"> 
    <div id="fooDiv"> 
     <div class="bar"> 
     </div> 
    </div>   
</script> 

呈現的佈局和顯示區域代碼:

var FooLayout = require('./browserify/path'); 
var fooLayout = new FooLayout({el:"#fooDiv"}) 

collectionView = new CollectionView({ 
    collection: collection 
}); 
fooLayout.render(); 
fooLayout.barRegion.show(collectionView); 

我得到一個錯誤未被捕獲的錯誤: DOM中的「el」#foo .bar必須存在

我在LayoutView的功能中缺少什麼?我有一個類似的例子工作得很好,但由於某種原因,我無法複製它。

回答

4

發生這種情況是因爲視圖與DOM分離。如果指定{el:"#fooDiv"},則#fooDiv元素必須位於DOM中。我想應該是這樣的:

<script id="fooTemplate" type="text/template"> 
    <div class="bar"></div>  
</script> 

添加#fooDiv HTML標記

<body> 
    ... 
    <div id="fooDiv"></div> 
    ... 
</body> 

,然後你可以做

// "wrap" new Layout around existing div 
new FooLayout({ el: '#fooDiv' }); 
// etc. 

// create a new DOM element with the id 'fooDiv': 
var fooLayout = new FooLayout({ id: 'fooDiv' }); 
fooLayout.render(); 
document.body.appendChild(fooLayout.el); // or $('body').append(fooLayout.el);