简体   繁体   English

使用Bacon.js在容器中列出

[英]List in a Container with Bacon.js

I have a list of elements and a container for the list. 我有一个元素列表和一个列表容器。 (They are implemented with Marionette + Backbone views). (它们通过Marionette + Backbone视图实现)。

What I'd like to do is register a click on the list and send it to the container with bacon.js or something similar. 我想要做的是在列表上注册一个单击,然后使用bacon.js或类似的东西将其发送到容器。

This is fairly easy with something like: this.selected = this.$el.asEventStream('click').map(this); 使用以下代码相当容易: this.selected = this.$el.asEventStream('click').map(this); , so that I can highlight the selected item. ,以便突出显示所选项目。

The problem now is that as soon as another element is selected the old one must be unselected, and I'm not sure what strategy to use here. 现在的问题是,一旦选择了另一个元素,就必须立即取消选择旧的元素,而且我不确定此处要使用哪种策略。

I'd like not to mutate the list from the container. 我不想更改容器中的列表。

Edit to clarify: 编辑以澄清:

The code I have at the moment is here: 我目前拥有的代码在这里:

https://github.com/archaeron/Marionette.CompoundView https://github.com/archaeron/Marionette.CompoundView

And I'm still doing this with traditional events, but I'd like to try another way of doing it. 而且我仍然会通过传统活动来做到这一点,但我想尝试另一种方式。

I don't know how to implement it correctly with Backbone, but with Bacon alone I would do something like this: 我不知道如何使用Backbone正确实现它,但是仅使用培根,我会做这样的事情:

$container = $('.container')

itemClicks = $container.asEventStream('click', '.item').map (event) ->
  $(event.target)

nonContainerClicks = $(document).asEventStream('click').filter (event) ->
  not $.contains($container.get(0), event.target)

selectedItem = Bacon.mergeAll(
  itemClicks,
  nonContainerClicks.map(-> null)
).toProperty(null)

selectedItem.onValue (itemEl) ->
  $('.container').find('.item.selected').removeClass('selected')
  if itemEl isnt null
    itemEl.addClass('selected')

Don't sure if it helps, but may be will give you some thoughts at least. 不确定是否有帮助,但至少可以给您一些想法。

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

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