【问题标题】:Layout and Regions using require js, Backbone, Marionette and Underscore js使用 require js、Backbone、Marionette 和 Underscore js 的布局和区域
【发布时间】:2023-03-28 05:06:01
【问题描述】:

我正在尝试使用木偶来掌握 Backbone 和 Require JS 的一些出色功能。但是,我发现该应用程序可用于在模板中呈现视图存在一些问题:

这是我的 Js 文件:

define(
    ['jquery',
     'bootstrap',
     'backbone',
     'marionette',
     'jquery-ui',
     'text!templates/admin/home.html',
     'js/views/admin/sidebar',
     'js/views/admin/mainContent'
    ], 
    function(
        $,
        Bootstrap,
        Backbone, 
        Marionette,
        JqueryUI,
        ViewMainLayout,
        ViewSidebarMenu,
        ViewMainContent
    ) {
        var TestApp = new Backbone.Marionette.Application();

        TestApp.addInitializer(function(){
            var layoutView = new AppLayoutView();
            layoutView.render();
            TestApp.appRegion.show(layoutView);

            layoutView.menu.show(ViewSidebarMenu);
            layoutView.content.show(ViewMainContent);
        });


         var AppLayoutView = Backbone.Marionette.Layout.extend({
             template: "#layoutPage",

             regions: {
                 menu: "#sideMenuBar",
                 content: "#MainContent"
              }
          });



        TestApp.addRegions({
            appRegion: "#homepage"
         });

        TestApp.start();
});

这是我的 html 文件:

<div id="homepage"></div>

<script type="text/template" id="layoutPage">       

<div id="sideMenuBar"> --- Loading Sidebar Menu --- </div>

<div id="MainContent"> --- Loading Content --- </div>

<div class="clearfix">&nbsp;</div>
</script>

浏览器控制台向我抛出错误: 错误:在backbone.js 文件中找不到模板:“#homepage”。

知道是什么原因造成的吗?

【问题讨论】:

    标签: marionette


    【解决方案1】:

    很明显是什么导致了您的问题 - 主干在您的 html 中找不到 #homepage。我假设当您说“这是我的 html 文件”时,您指的是您的模板 (text!templates/admin/home.html),而不是浏览器加载的页面上的 html?如果是这种情况,那么请务必注意,在您的视图中,您没有将任何内容从模板加载到 DOM 中,这可能就是 Marionette 找不到它的原因。

    有几种方法可以结合 requireJS 来做到这一点,但我通常只是使用 jQuery 将模板直接加载到 DOM 中,如下所示:

    $("body").after(ViewMainLayout)
    

    【讨论】:

    • 感谢您的快速回复。根据您的建议,我直接将模板加载到 DOM 中并得到错误:无法呈现模板,因为它是 false、null 或 undefined。
    • 我认为他的 html 指的是 index.html ,即页面上已有的内容。由于他通过 声明了#layoutPage 模板
    • 糟糕,我收回了,它看起来确实是 text!templates/admin/home.html 的内容
    猜你喜欢
    • 2012-12-02
    • 1970-01-01
    • 2014-03-05
    • 2013-04-12
    • 2014-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多