【问题标题】:Backbone Marionette Layout and Composite View Not Rendering骨干木偶布局和复合视图未呈现
【发布时间】:2013-11-12 14:02:26
【问题描述】:

我有一个包含许多子视图的视图。我的计划是使用 Marionette 的布局对象作为骨架视图,如下所示:

define([
  'app',
  'views/orders/list',
  'text!templates/orders/main.html'
],
function (app, ListView, template) {
  'use strict';

  var View = Backbone.Marionette.Layout.extend({
      template : _.template(template),

      id : 'orders',

      regions : {
          list : '#list'
      },

      initialize : function () {
          this.listView = new ListView({
            collection : this.collection
          });

          // And render it here? Seems like a bad idea.
          this.list.show(this.listView);
      }
  });

  return View;
});

ListView 是我创建的复合视图:

define([
    'app',
    'views/orders/list-item',
    'text!templates/orders/list.html'
],
function (app, ListItem, template) {
  'use strict';

  var View = Backbone.Marionette.CompositeView.extend({
    template : _.template(template),

    itemView : ListItem,
    itemViewContainer : '.items',
  });

  return View;
});

问题是当我将 listView 传递给该区域时,它不会使用我的集合中的项目列表呈现。它只呈现包装器。如果我直接调用 listView 上的 render 方法并检查元素...一切都正确构造。

布局代码是否会阻止复合视图正确呈现?我在错误的地方渲染吗?也许这甚至不是工作子视图的正确方法?

任何帮助表示赞赏!

注意:出于测试目的,集合是由硬编码值组成的。

【问题讨论】:

    标签: backbone.js marionette


    【解决方案1】:

    在你初始化 Layout 的时候它的 HTML 还没有渲染,改变 Layout 的 onRender 函数的初始化代码,那个时候 HTML(区域)就会在那里

    var View = Backbone.Marionette.Layout.extend({
      template : _.template(template),
    
      id : 'orders',
    
      regions : {
          list : '#list'
      },
    
      onRender: function () {
          this.listView = new ListView({
            collection : this.collection
          });
    
          // And render it here? Seems like a bad idea.
          this.list.show(this.listView);
      }
    

    });

    【讨论】:

    • 这是正确答案,它有效,谢谢!虽然我看不到木偶网站上记录的“onRender”。我猜它来自布局扩展的 ItemView 对象。
    • 实际上布局视图是从 Marionette.View 扩展而来的,但是是的,您可以获得与 ItemView 几乎相同的功能。
    猜你喜欢
    • 1970-01-01
    • 2014-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-18
    • 1970-01-01
    • 2023-04-09
    相关资源
    最近更新 更多