acdlite / acdlite/flummox

Action creator or store for performing save/update ajax

未關閉
#227 3 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視
outdated question
主要語言
JavaScript
星號
1.7k
分支
109
PR 合併指標
30 天內沒有已合併 PR

描述

Hi,
im struggling with deciding where to trigger my save/update AJAX calls from. In your examples all the I/O is done in the action creator. This makes a lots of sense for initial state, making sure the flux instance is loaded before rendering etc, but in cases where your true state is in the store im not sure if I follow this pattern. Ok time for example.

My store is as follows:

``` javascript
class MyStore extends Store {

constructor(flux) {
super();
const actionIds = flux.getActionIds(FluxConst);
this.register(actionIds.middleNameChanged, this.onMiddleNameChanged);

this.state = {
person : Immutable.Map({ firstName:'John', lastName: 'Doe',middleName:''})
};
}
onMiddleNameChanged(newMiddleName){
this.setState(
{
person: this.state.person.set('middleName',newMiddleName)
});
}
}
```

The store holds a single person object represented using an immutable map.
Next is my view. This is a simple form showing the person with an input to set a middle name( the view is wrapped in a further up the chain).

``` javascript
const Component = React.createClass({
middleNameChanged: function(event) {
this.props.flux.getActions(FluxConst).middleNameChanged(event.target.value);;
},
savePerson(){
this.props.flux.getActions(FluxConst).savePerson();
},
render(){
return(


First name: {this.props.person.get('firstName')}


Surname: {this.props.person.get('surName')}


Please enter middlename:



Save me

);
}
});
```

And finally my action creator.

``` javascript
class MyActions extends Actions{
async savePerson(person){
return doSaveAjaxThatReturnsPromise(person);
}
middleNameChanged(middleName){
return middleName;
}
}
```

As you can see the action creator got two actions. One of them receiving an update when the user makes a change in the input.
The second actions is a "savePerson" actions.

Here is my problem. My store owns the state, so it seems to me that it should be the store that passes the "person to save" to the action creator. But in all examples following this pattern the data is passed from from "view" -> "action creator" -> "store". This makes my view responsible for passing data to the server, even though its my store thats the "owner" of this person.

My first take on this was to trigger a "saveRequest" action to the store and let the store trigger the "savePerson" action with the "true-person" as payload,b ut this also seems a bit of.
So right now im leaning against using the action creator for all the initial data fetching( using the component static method pattern from your doc app example) and performing the entire save/update inside the store.

Any ideas on how to handle this?

貢獻指南

開啟貢獻指南

研究方向

The issue discusses Flux architecture with Flummox, focusing on where to trigger AJAX calls for saving data. The user provides code snippets for a store, view, and action creator. To understand the codebase, look at the Flummox source, especially the Actions and Store classes. Examine existing examples to see how data flows. Determine if the store should own the save logic or if the action creator should handle it. The goal is to propose a pattern for save/update AJAX calls that aligns with Flux principles.

由索引模型根據 Issue 內容生成。

評估

技術堆疊
javascript, react
領域
backend-api-design, frontend
Issue 類型
功能
難度
4/5
預估耗時
3-5 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。