简体   繁体   English

直接倾听组件中的Reflux Actions

[英]Listening directly to Reflux Actions in Component

From what I've read the pattern is the Components pass data to the Actions which Pass to the Store whose value changes trigger updates in Components that subscribe to the Stores. 从我所看到的模式是组件将数据传递给传递到商店的操作,其值更改会触发订阅商店的组件中的更新。 My question is how to "react" to these triggered updates in the form of a notification? 我的问题是如何以通知的形式对这些触发的更新做出“反应”? ( ie a successfully saved notification ) (即成功保存的通知)

Ie do I add logic to the render of this notification component that only displays itself if there is a some flag attribute in the object that its subscribed to? 即,我是否将此逻辑添加到此通知组件的呈现中,如果在其订阅的对象中存在某个标记属性,则该逻辑仅显示自身? Then deletes itself after a time. 然后在一段时间后删除自己。 This sounds wrong. 这听起来不对。

UPDATE UPDATE

Thanks to Hannes Johansson I think I have a better grasp of a pattern. 感谢Hannes Johansson,我想我对模式有了更好的把握。 What I have working is the following: 我的工作如下:

  1. Component passes data through action to the Store 组件通过操作将数据传递到Store

  2. The Store interacts with the api and adds a flag to the model that the component is now notified of an updated model. Store与api交互并向模型添加一个标志,该组件现在会通知更新的模型。

     createItem: function (item) { $.ajax({ url: '/items', method: 'POST', data: item, success: function (item) { CurrentBrandActions.addCampaign(item); this.item = item; item.newlyCreated = true; this.trigger(item); }.bind(this) }) } 
  3. The Component sees the flag and renders a "Notification Child Component" 组件看到标志并呈现“通知子组件”

     var newlyCreated = this.state.item.newlyCreated === true; if (newlyCreated) { newlyCreated = <ItemCreatedNotification item={this.state.item} /> } else { newlyCreated = ''; } return ( <form onSubmit={this.createItem} className="form"> {newlyCreated} 
  4. Something needs to move the app to a new place based on this event. 有些东西需要根据此事件将应用移动到新的位置。 Should this be a) the Notification Child Component b) Parent Component c) The Store? 这应该是a)通知子组件b)父组件c)商店?

According to Colin Megill's talk on flux api patterns the api interaction should occur in the Action, but reflux doesn't really allow for that. 根据Colin Megill关于flux api模式讨论,api相互作用应该在Action中发生,但回流并不能真正实现。

UPDATE 2 更新2

  1. Component passes data to an Action called createItemRequest 组件将数据传递给名为createItemRequest的Action

  2. The Action has a preEmit hook that actually does the api call. Action有一个preEmit钩子,实际上是api调用。 The createItemRequest continues to the Store so that the store can change the model to reflect the state of sending which is then displayed in the component( maybe show a spinner ). createItemRequest继续到Store,以便商店可以更改模型以反映发送状态,然后显示在组件中(可能显示微调器)。 The Action is also responsible for firing two other events depending on the api result. Action还负责根据api结果触发另外两个事件。

     ItemActions.createItemRequest.preEmit = function (data) { $.ajax({ url: '/items', method: 'POST', data: data, success: function (item) { ItemActions.itemCreatedSuccess(item); }, error: function (error) { ItemActions.itemCreatedError(error); } }); } 

There are different approaches to this. 有不同的方法。 For example, in Reflux it's very easy to listen directly to actions if you choose to, since each action is actually a "dispatcher". 例如,在Reflux中 ,如果您选择,则可以非常轻松地直接监听操作,因为每个操作实际上都是“调度程序”。

However, the general, purist Flux principle is that only stores register with the dispatcher and that components only listen to store updates. 但是,一般的,纯粹的Flux原则是只有商店向调度员注册,而且组件只监听商店更新。 And the store just trigger an event that notifies that something has changed, not providing any payload. 而商店只是触发一个事件,通知事情发生了变化,没有提供任何有效载荷。 Then it's up to the component to read the store's state and determine how to render it. 然后由组件来读取商店的状态并确定如何呈现它。

One approach would be the one you describe, put some flag on the items in the store to signal that an update has happened, but it would violate the Flux principle if the components themselves then update the stored items' flags, since only stores are meant to mutate state, and only in response to actions and not from any other source. 一种方法是你描述的方法,在商店中的项目上放置一些标志以表示发生了更新,但如果组件本身随后更新存储项目的标志,则会违反Flux原则,因为只有商店意味着改变状态,只是为了响应行动,而不是来自任何其他来源。 So in that case the "Flux thing" to do would probably be to trigger yet another event that signals that the newly added item has been noted so that the store can then reset the flag in response to that action. 因此,在这种情况下,要做的“Flux事情”可能是触发另一个事件,该事件表示已经记录了新添加的项目,以便商店可以重置标志以响应该动作。

Another approach I can think of would be to diff the state in your component when it gets notified of a store update. 我能想到的另一种方法是在获得商店更新通知时在组件中区分状态。 Then keep the flags only in the component, or even keeping newly added items in a separate list in the state and render them separately. 然后仅在组件中保留标志,或者甚至将新添加的项保存在状态的单独列表中并单独呈现它们。

There are no hard rules here, except that if you want to follow the core Flux principle, components should never directly mutate stores' state, because that should only be mutated by the stores themselves in response to actions. 这里没有硬规则,除非你想要遵循核心Flux原则,组件永远不应该直接改变商店的状态,因为只有商店本身才能对其进行突变以响应操作。 That allows for a uni-directional data flow and a single source of truth about the data, which is the main goal of Flux. 这允许单向数据流和关于数据的单一真实来源,这是Flux的主要目标。

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

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