简体   繁体   English

骨干木偶,两次渲染复合视图?

[英]Backbone Marionette, rendering composite view twice?

I have this issue that prints user data twice, this is the code: 我有两次将用户数据打印两次的问题,这是代码:

window.UserData = {}
UserData.Model = Backbone.Model.extend();
UserData.Collection = Backbone.Collection.extend({
  url: '/test.php?c=userData',
  model: UserData.Model,
  parse: function (response) {
    return response.data;
  }
});
UserData.View = Backbone.Marionette.CompositeView.extend({
  tagName: 'span',
  template: Handlebars.compile($('#user-data-template').html())
});
BaseApp.Layout = Backbone.Marionette.Layout.extend({
  template: Handlebars.compile($('#base-layout-template').html()),
  regions: {
    'region_user_data': '#user_data_reg'
  },
  initialize: function () {
    _.bindAll(this);
  },
  onRender: function () {
    var self = this;
    var c = new UserData.Collection();
    c.fetch({
      success: function () {
        var ud = new UserData.View({
          collection: c
        });
        self.region_user_data.show(ud);
      }
    });
  }
});

this BaseApp Layout is being rendered once on a region (in body). 此BaseApp布局仅在区域(主体)上呈现一次。

This is the template: 这是模板:

<script id='user-data-template' type='text/x-handlebars-template'>
    <a href="#" id="logout"> Logout </a> Hola {{uname}}
</script>

<script id='base-layout-template' type='text/x-handlebars-template'>
    base page layout
    <div id="user_data_reg" class="user_data_reg"></div>
</script>

this is what is being shown in the browser (chrome): 这是浏览器(chrome)中显示的内容:

Logout Hola Logout Hola 14

As you can see it displays the user layout twice but the first time does not displays the uname, it is kind of weird because in chrome I have this: 如您所见,它两次显示用户布局,但是第一次不显示用户名,这有点奇怪,因为在chrome中,我有以下内容:

child {models: Array[0], length: 0, _byId: Object, 
constructor: function, url: "http://localhost/test.php?c=userData"…}

the model is being displayed only 1 and what it does is that prints twice content. 该模型仅显示1,它的作用是打印两次内容。

Also when I fetch the collection data I do this: 另外,当我获取收集数据时,我也会这样做:

c.fetch({
  success: function () {
    var ud = new UserData.View({
      collection: c
    });
    self.region_user_data.show(ud);
    console.log("print xx");
  }

and it only logs "print xx" only once & not twice as I expected. 而且它只记录一次“打印xx”,而不是我预期的两次。

Why am I getting double userData content even if the collection is fetching only a single model, why is this happening? 为什么即使集合仅获取单个模型,我也得到双userData内容,为什么会发生这种情况? Regards. 问候。

This was an issue with the view type, instead of using "CompositeView" I switched to ItemView and the problem went away. 这是视图类型的问题,而不是使用“ CompositeView”,我切换到了ItemView,问题消失了。 I guess Marionette forces to use ItemView for single data. 我猜木偶会强制对单个数据使用ItemView。

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

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