【发布时间】:2014-03-05 05:03:03
【问题描述】:
基本上,我有一个应用布局,其中包含特定路线的通用页眉和页脚部分。问题是当子路由被击中时,我希望能够转换此布局的主要内容区域。
示例:#products 是主路由,#products/:id 是子路由。
在此模块的控制器中,我使用 require.js 来获取 #products 路由的视图,并显示带有全局页眉和页脚的着陆点作为此布局区域的一部分。我还定义了一个内容区域,一旦 id 包含在路由中,我就想将其转出。那么一旦这条新路线被击中,我如何在视图上调用方法呢?当父路由被命中时,我是否需要缓存应用程序的当前状态,然后在子路由被命中时引用它?当子路由被击中并且用户没有访问父路由时,我是否还需要初始化视图?
路由器
define(['backbone', 'marionette', 'c_controllers/Controller'], function ( Backbone, Marionette, Controller ) {
'use strict';
var AppRouter = Backbone.Marionette.AppRouter.extend({
appRoutes : {
// PRODUCT ROUTES
'product' : 'product',
'product/:id' : 'showPlp'
}
});
return new AppRouter({'controller': Controller});
});
控制器
define(["backbone", 'marionette'], function (Backbone, Marionette) {
'use strict';
return {
product : function( id ) {
require(['c_product/product',
'app_views/menu'], function( Product, Menu ) {
APP.menu.show( new Menu() );
APP.page.show( new Product() );
});
}
};
});
查看
define([
'backbone', 'marionette', 'app_views/globalNav',
'c_product/productLanding', 'text!productNavTemplate', 'text!productBaseTemplate'], function( Backbone, Marionette, GlobalNav, ProductLanding, ProductNavTemplate, ProductBaseTemplate ) {
var product = Backbone.Marionette.Layout.extend({
id : 'product',
template : _.template( ProductBaseTemplate ),
regions : {
nav : '#globalNav',
content : '#view-content',
footer : '#footer'
},
events : {
},
initialize : function() {
},
onRender : function() {
// extend the nav view for the template that matches this flow
var Nav = GlobalNav.extend({ template : _.template( ProductNavTemplate )});
// show the nav, main content, and footer
this.nav.show( new Nav() );
this.content.show( new ProductLanding() );
}
});
return product;
});
【问题讨论】:
标签: javascript backbone.js routes marionette