【问题标题】:Backbone/Marionette - How to define the correct RegionBackbone/Marionette - 如何定义正确的区域
【发布时间】:2014-05-02 08:58:48
【问题描述】:

我遇到了一个问题,我找不到任何文档,所以我不确定是否可以这样做。

我有一个运行良好的主干应用程序,它基于传入 pubsub 节点的 iq 数据包为我构建了一个“井”列表(我们称之为主干应用程序 1)。

在这个主干井模板的末尾是以下内容:

<div class='row' id='row-participants-<%= chatid %>' style='display: none'>
</div>

这是一个 div,我希望将参与者列表附加到其中(在加载文档时不存在,并且 MyApp.start 是调用)。

这个参与者列表是使用骨干木偶构建的,它基于另一组传入的 iq 数据包,其中列出了特定聊天 ID 的参与者(我们称之为骨干App2)。

因此,在定义要使用的主干区域时,它分为两部分。

首先,您可以在其中添加区域并定义唯一名称和 DOM:

MyApp.addRegions({
    participantsContainer... : "#row-participants-..."
})

第二个是你告诉主干显示视图的地方:

MyApp.participantsContainer.show(aParticipantsView);

所以在我上面的场景中,我需要动态创建区域(我在哪里做这个?这可以在 initialize 中为backboneApp1 完成,因为我是将 chatid 传递到我的井集合中的每个模型中?或者可能在 pubsub 处理程序函数中,在我将数据发送到主干之前?)。

然后我需要告诉backboneApp2到show()根据chatid在正确的区域,这是我卡住的地方(chatid也通过了,每个集合中的模型有一个参与者名称值和一个与之关联的聊天标识符值)。 如果我打算将参数传递给它,我会将定义区域的第二部分放在哪里?

请询问是否需要进一步解释。

谢谢。

亲切的问候,

加里·舍吉尔

编辑:这似乎是解决方案,但我不确定如何在没有下划线Backbone: A list of views inside a view的情况下做到这一点

我的代码:

创建参与者数组

var participants = [];
$(iq).find('participants').each(function() {
    var participantsNodes = this.childNodes;
    for (var i = 0; i < participantsNodes.length; i++) {
        var participantAttr = participantsNodes[i].attributes
        var participant = participantAttr[0].nodeValue;
            participants.push({"name": participant, "chatid": chatid});
    }
});

model.set({
    participants : participants,
    chatid : chatid
    ...
});

项目视图

GroupChatView = Backbone.Marionette.ItemView.extend({
    template : "#template-groupchat",
    tagName : 'div',
    className : 'alert alert-custom',

    initialize: function(){
        this.$el.prop("id", this.model.get("chatid"));
    },

    ...

群聊模板

<script type="text/template" id="template-groupchat">

    <a id='close-<%= chatid %>' class='close hide' data-dismiss='alert' href='#'>
        &times;
    </a>
    ...
    <div class='row' id='row-participants-<%= chatid %>' style='display: none'>
<!-- CUSTOM CODE SHOULD BE HERE? -->
    </div>

</script>

【问题讨论】:

  • 你有代码要显示吗?
  • 嗨,gerf,上面的代码说明了您通常如何告诉骨干在哪里添加集合。所以在上面写着MyApp.participantsContainer.show(aParticipantsView); 的地方,participantsContainer 是要添加到的区域。有没有办法让这个区域由模型的 id 定义?所以它只附加到有问题的模型
  • 也许我想错了?这是用例:我使用主干构建一组 div,每个 div 代表一个群聊。在更改传递给模型的参与者值(其中模型是群聊)时,我需要更新群聊模型视图中的参与者视图以反映新的参与者列表。这有意义吗?
  • Regions 可能不是您想要用于此目的的。如果您正在创建行,您可能想要使用 CollectionView 或 CompositeView。
  • 环顾四周,发现这篇文章stackoverflow.com/questions/11209030/… 这几乎就是我想要做的,在模型中渲染一个列表......我不知道该怎么做我自己,因为我从未使用过下划线。将相关代码添加到主帖。

标签: javascript backbone.js marionette region


【解决方案1】:

解决了使用下划线在模板中渲染集合。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-05
    • 2012-08-17
    • 2013-04-12
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    相关资源
    最近更新 更多