這將有助於更多地瞭解你要做什麼,但這裏有一個答案。讓我知道它是否有幫助!
比方說,這是你的HTML佈局:
<div id="app">
<div id="app_nav">
<a href="#music">Music</a>
<a href="#books">Books</a>
</div>
<div id="sub_app"></div>
</div> <!-- /#app_main -->
對於我們的 「音樂」 子應用程序,我們將使用這個模板:
<script id="musicApp-template" type="template/html">
<div class="albums"></div>
<div class="artists"><div>
</script>
對於專輯項目視圖:
<script id="album-template" type="template/html">
<img src="<%=albumThumbSrc %>" />
<p><%=albumTitle %></p>
</script>
對於藝術家項目視圖:
<script id="artist-template" type="template/html">
<%=firstName %> <%=lastName %>
</script>
-
對於我們的 「書」 的子應用程序,我們將使用這個模板:
<script id="booksApp-template" type="template/html">
<div class="books"></div>
<div class="authors"></div>
</script>
對於書項目視圖:
<script id="book-template" type="template/html">
<img src="<%=bookThumbSrc %>" />
<p><%=bookTitle %></p>
</script>
對於藝術家項目查看:
<script id="author-template" type="template/html">
<%=firstName %> <%=lastName %>
</script>
並且自舉程序:
$(document).ready({
myApp.start();
if(!Backbone.history.started) Backbone.history.start();
});
-
我們建立我們的木偶意見。
在myApp.js
var myApp = new Backbone.Marionnette.Application();
myApp.addRegions({
subAppRegion: "#sub_app"
});
// Your data
myApp.artists = new Backbone.Collection([...]);
myApp.books = new Backbone.Collection([...]);
在myApp.musicApp.js
myApp.module("musicApp", function(musicApp, myApp) {
/* Setup your Views
================== */
var MusicLayout = Backbone.Marionette.Layout.extend({
template: #musicApp-template",
regions: {
albumsRegion: ".albums",
artistsRegion: ".artists"
}
});
var AlbumView = Backbone.Marionette.ItemView.extend({
template: "#album-template"
});
var AlbumListView = Backbone.Marionette.CollectionView({
itemView: AlbumView
});
var ArtistView = ...
var ArtistListView = ...
/* Create router for app navigation
==================================== */
var Router = Backbone.Marionette.AppRouter.extend({
appRoutes: {
"music" : showMusicApp
},
controller: musicApp
});
new Router();
/* Method to show the music app
================== */
musicApp.showMusicApp = function() {
// Instantiate Views
var musicLayout = new MusicLayout();
var albumListView = new AlbumListView({ collection: myApp.albums });
var artistListView = new ArtistsListView({ collection: myApp.artists });
// Show musicApp layout in subAppRegion of main app
myApp.subAppRegion.show(musicLayout);
// Show albums and artists in musicApp layout
layout.albumsRegion.show(albumListView);
layout.artistsRegion.show(artistListView);
}
});
你可以設置你的myApp.booksApp
模塊幾乎相同的方式:
myApp.module("booksApp", function(booksApp, myApp) {
...
var Router = Backbone.Marionette.AppRouter.extend({
appRoutes: {
"books" : showBooksApp
},
controller: booksApp
});
new Router();
booksApp.showBooksApp = function() {
...
myApp.subAppRegion.show(booksLayout)
...
}
...
});
我的天堂」 t測試了所有這些代碼,所以很抱歉,如果有一些扭曲,我相信它可以改進。
如果你還沒有讀David Sulc's tutorial,你應該看看 - 它會讓你更好地瞭解一個完整的應用程序。但我希望這給你一個基本的想法,你如何使用佈局和區域來顯示不同的子應用程序視圖。
使用事件通過視圖進行通信。在模型上引發這些事件(這樣您甚至可以通過收集來訂閱它們) – Deeptechtons 2012-07-09 11:12:44