這是,我迄今爲止所取得的成就。我在我的第一個模板中有一個div。當我在該div內顯示另一個layoutView時。它顯示以下錯誤。Marionette嵌套的LayoutView不呈現,顯示錯誤「元素必須存在於DOM中」
Uncaught Error: An "el" #nestedDiv must exist in DOM
HTML -
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>MarionetteJS</title>
</head>
<body>
<div id="container"><div>
<script type='text/template' id='myTemplate'>
<h2><%=heading%></h2>
<div id='nestedDiv'></div>
</script>
<script type='text/template' id='innerTemplate'>
<h2><%=nestedHeading%></h2>
</script>
<script src='_assets/js/_lib/jquery-1.7.2.min.js'></script>
<script src='_assets/js/_lib/underscore.js'></script>
<script src='_assets/js/_lib/backbone.js'></script>
<script src='_assets/js/_lib/backbone.marionette.js'></script>
<script src='_assets/js/layoutView.js'></script>
</body>
</html>
JS -
//Application Object
var myApp = new Marionette.Application({
regions: {
main: '#container'
}
});
//First Model
var TaskModel = Backbone.Model.extend({
defaults: {
'heading' : 'Welcome to Backbone'
}
});
//Second Model
var Person = Backbone.Model.extend({
defaults: {
'nestedHeading' : 'This is a subheading.'
}
});
//View for Div #nestedDiv
var PersonView = Marionette.ItemView.extend({
template: '#innerTemplate'
});
//View for main Region
var TaskView = Marionette.LayoutView.extend({
template : '#myTemplate',
onShow: function() {
var person = new Person();
var personView = new PersonView({model: person});
var PersonLayoutView = Marionette.LayoutView.extend({
regions: {
'foo' : '#nestedDiv'
}
});
var obj = new PersonLayoutView();
obj.foo.show(personView);
}
});
var taskModel = new TaskModel();
var taskView = new TaskView({model:taskModel});
myApp.main.show(taskView);
,這裏是JSBin鏈接 - http://jsbin.com/dusica/1/edit?html,js,console,output