admin管理员组文章数量:1334697
I'm building a simple React Flux js app similar to TodoList, which basic functionality lies in CRUD operations on some entities. When app starts it fetches the data from server and shows the list of items, then I can create new item using form, edit and delete. I decided to try Immutable.js approach but faced the question: when and which data should I convert to immutable objects. For example when I fetch the list, I make it immutable(Immutable.fromJS()) and then assign to store's state, right? But then I create new item, get plain object from the form fields and should somehow post this data to server. In order to avoid loading the list again I want to add this new item to immutable store list. So how should I deal with such situations when I have to convert some objects to immutable for app use, and fetch and send data to server using plain json?
I'm building a simple React Flux js app similar to TodoList, which basic functionality lies in CRUD operations on some entities. When app starts it fetches the data from server and shows the list of items, then I can create new item using form, edit and delete. I decided to try Immutable.js approach but faced the question: when and which data should I convert to immutable objects. For example when I fetch the list, I make it immutable(Immutable.fromJS()) and then assign to store's state, right? But then I create new item, get plain object from the form fields and should somehow post this data to server. In order to avoid loading the list again I want to add this new item to immutable store list. So how should I deal with such situations when I have to convert some objects to immutable for app use, and fetch and send data to server using plain json?
Share edited May 26, 2015 at 14:06 beshanoe asked May 26, 2015 at 6:03 beshanoebeshanoe 2332 silver badges8 bronze badges 1- You can also check out github./engineforce/ImmutableAssign, which supports immutability and allows you to continue working with POJO (Plain Old JavaScript Object). – engineforce Commented Jun 21, 2016 at 14:30
1 Answer
Reset to default 12You want to make all your objects immutable. The only time you convert it to a mutable object is when you need to send it to the server. And since you get back a plain JSON object from the server, you convert that to an immutable object when you fetch it.
When you create a new item, you want an immutable object as well, which you send to the store. So the state of the store is just a bunch of immutable objects inside an immutable vector. The fact that you need to send JSON objects to the server is an implementation detail of the munication with the server, and that should only be known by the store/service that handles the munication.
So the flow would be:
- Fetch items from the server as a JSON array of objects
- Convert that JSON array to an immutable vector of immutable records
- Pass that immutable vector to your ponents to render them
- When editing/creating new items, make sure you only deal with immutable records (that is, keep an immutable
newItem
record onthis.state
in the ponent, and update that record when form fields change). - When the user hit Save, send
this.state.newItem
to the store, which adds the item to its immutable vector and also converts it to JSON and sends it to the server.
本文标签: javascriptImmutablejs converting fetched data to immutableStack Overflow
版权声明:本文标题:javascript - Immutable.js converting fetched data to immutable - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742262991a2442864.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论