我在渲染 Marionette LayoutView 和显示该布局内的区域时遇到问题。
我的布局文件:
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);
我收到一个错误 Uncaught Error: An "el"#foo .bar must exist in DOM
我在 LayoutView 的功能中缺少什么?我有一个类似的例子工作得很好,但出于某种原因我无法复制它。
最佳答案
发生这种情况是因为 View 与 DOM 分离。如果指定 {el:"#fooDiv"}
,#fooDiv 元素必须在 DOM 中。我认为应该有这样的东西:
<script id="fooTemplate" type="text/template">
<div class="bar"></div>
</script>
在 html 标记中添加#fooDiv
<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);
关于javascript - Backbone Marionette LayoutView 找不到 DOM 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26745674/