简体   繁体   English

如何在Backbone.Marionette中集成菜单子页面

[英]How to integrate menu sub page in Backbone.Marionette

I am building a mobile web application using jQuery mobile 1.4.0 and Backbone.Marionette. 我正在使用jQuery mobile 1.4.0和Backbone.Marionette构建移动Web应用程序。 I have some views, including a CollectionView, an ItemView for details and an ItemView for editing. 我有一些视图,包括CollectionView,用于详细信息的ItemView和用于编辑的ItemView。 Currently, I'm using only the content-section of my main jQuery mobile page for rendering stuff with Marionette and Backbone (as a region) I have a static menu, which also is a jQuery mobile page. 当前,我仅使用我的主要jQuery移动页面的content-section来渲染带有Marionette和Backbone(作为区域)的内容,我有一个静态菜单,它也是jQuery移动页面。

It looks like this: 看起来像这样:

<section data-role="page" id="main">
    <header data-role="header" data-position="fixed">
        <h1 id="heading">Liste</h1>
            <a id="menu-btn" href="#/menu" class="ui-btn-right">Menu</a>
    </header>

    <section class="ui-content" data-role="main" id="content">
        <!-- a Marionette.Region for displaying error messages -->
        <div id="message"></div>
        <!-- This is the main Region -->
        <div id="page"></div>
    </section>
</section>

<!-- this is the jquery Mobile menu page. It has it's own header -->
<section data-role="page" id="menu">
    <header data-role="header" data-position="fixed">
        <a href="#" data-icon="back" data-iconpos="notext">Zurück</a>
        <h1>Menü</h1>
    </header>

    <section class="ui-content" data-role="main" id="menu-page">
        <ul data-role="listview">
            <!-- the href should be replaced with a click-event -->
            <li><a href="#/users/new" class="ui-icon-plus">Neu</a></li>
        </ul>
    </section>
</section>

I have my menu route in my Backbone.Router (jqm routing is disabled) that shows the menu page with $.mobile.changePage. 我在Backbone.Router(已禁用jqm路由)中显示菜单路线,该菜单路线显示带有$ .mobile.changePage的菜单页面。 Currently, the content of the menu page is static (it contains only the "new" option). 当前,菜单页面的内容是静态的(它仅包含“新”选项)。 Now I want to make my menu dynamic so that it shows options which are valid actions for the view that is currently displayed in the main region. 现在,我想使菜单动态化,以便它显示选项,这些选项是当前在主区域中显示的视图的有效操作。

For example, when the details view is displayed, it should contain "edit", "delete". 例如,当显示详细信息视图时,它应包含“编辑”,“删除”。 In the list view, it should contain "new". 在列表视图中,它应包含“ new”。

I think I need a MenuView for this. 我想我需要一个MenuView。 But how should I check which menu items are for which view? 但是我应该如何检查哪个菜单项适合哪个视图? And when should I render the menu view? 我应该何时渲染菜单视图? Also, how can I get my view to react to when then the menu item was clicked? 另外,当我单击菜单项时如何对视图做出反应?

What would be the best approach for this? 最好的方法是什么?

Here is what I would do, 这就是我要做的

Use CollectionView for your menu 使用CollectionView作为菜单

Using a CollectionView for your menu gives you the ability to dynamically define your menu, your menu would be defined using a collection of models, something like this 对菜单使用CollectionView可以动态定义菜单,菜单将使用模型集合进行定义,如下所示

{
  lable : 'Home',
  href  : '/home',
  icon  : 'pic/home.png'
}

Have your controller update the menu 让您的控制器更新菜单

I assume your controller updates the content area when a route is triggered. 我假设您的控制器在触发路由时会更新内容区域。 You can have your controller re-render your menu using the a MenuItemCollection that has list of menu items related to the content being displayed. 您可以使用MenuItemCollection来让控制器重新呈现菜单,该MenuItemCollection具有与所显示内容相关的菜单项列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM