简体   繁体   English

Marionette / Backbone.js中的渲染布局和子视图

[英]Rendering Layouts and sub-views in Marionette / Backbone.js

I have a working solution in regard to rendering layouts with views in regions in a Marionette application I'm working on, but something doesn't feel right about it. 对于正在使用的Marionette应用程序中的区域中的视图渲染布局,我有一个有效的解决方案,但是有些不适。 Do you have to append anything directly to the DOM? 您是否必须直接将任何内容附加到DOM?

Here is my method in the controller: 这是我在控制器中的方法:

 //Grab the main Layout
        var layout = new APP.Views.LayoutView();

        //Render that layout
        layout.render();

         //Make the model
        var simpleModel = new APP.Models.simpleModel({
          "field1" : "foo",
          "field2" : "bar"
        });


        layout.header.show(new APP.Views.subView({model: simpleModel}));

        $('body').html(layout.el);

It's the last part that feels unnatural to me. 这是我觉得不自然的最后一部分。 This is primarily because if I move 'layout.header.show' to after the .html(), then it doesn't render properly. 这主要是因为如果我将'layout.header.show'移到.html()之后,那么它将无法正确呈现。 What's the point of regions if they aren't dynamically changable after pushing it to the DOM? 如果将区域推送到DOM后不能动态更改,那么区域的意义何在?

Here is my layout: 这是我的布局:

APP.Views.LayoutView = Backbone.Marionette.Layout.extend({

  template: "#layoutTemplate",

  className : 'wrapper',

  regions: {
    header: "#header",
    footer: "#footer"
  }


});

and here is the sub view: 这是子视图:

APP.Views.subView = Backbone.Marionette.ItemView.extend({

    template : '#headerTemplate',

    className: 'container'


});

As I said, this works, but it feels like I'm not using regions properly. 如我所说,这可行,但是感觉好像我没有正确使用区域。 Is there a better, more concise way to do this that will allow you access to regions after you rend the layout to the DOM? 有没有更好,更简洁的方法来执行此操作,使您可以在将布局重新分配到DOM后访问区域?

In the Marionette documentation there seems to be no mention of using .html() to get things on the page -- I'm wondering if this is left out because it's not needed or that it's assumed. Marionette文档中 ,似乎没有提到使用.html()在页面上显示内容的情况-我想知道是否由于不需要或假定该内容而将其遗漏了。

Can anyone help? 有人可以帮忙吗?

Okay, so it seems like this can be circumvented by creating a 'region' in your application, then using .show() to show the layouts inside of it. 好的,这样看来可以通过在应用程序中创建一个“区域”,然后使用.show()在其中显示布局来规避。

Here is a link to a fiddle I found on SO that helped: http://jsfiddle.net/TDqkj/6/ 这是我在SO上找到的小提琴的链接,该小提琴对您有帮助: http : //jsfiddle.net/TDqkj/6/

as well as to another question: Understanding layouts in Marionette for Backbone.js 以及另一个问题: 了解Marionette中Backbone.js的布局

The fiddle in particular has this code: 小提琴特别具有以下代码:

App = new Backbone.Marionette.Application();
App.addRegions({
   'nav': '#nav',
  'content': '#content'
});

Which the programmer than uses to add layouts to those regions -- meaning you never have to append to the DOM. 程序员用来向这些区域添加布局的人-意味着您永远不必附加到DOM。

If anyone else has a more elegant solution, please post! 如果其他人有更优雅的解决方案,请发布!

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

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