Comments (6)
You can trigger events in the listeners of child nodes.
var store = new Freezer({a: {b: [1,2,3]}}),
listener = store.get().a.b.getListener()
;
listener.on('sayHi', () => console.log('Hi!'));
listener.trigger('sayHi'); // logs Hi!
But those events won't go up in the tree.
The way freezer works, whenever you call a updating function in the tree, it triggers the update
event in the listeners of all the nodes from the node that is being updated to the top. But triggering an event in a node is a single operation that doesn't propagate through the tree like the updates do.
from freezer.
One little gotcha with this, that is easy to workaround when you know about it is:
var store = new Freezer({a: {b: [1,2,3]}}),
listener = store.get().a.b.getListener()
;
listener.on('update', () => console.log('Update'));
store.get().a.set("b", [1,2,4]) // does not trigger an event
from freezer.
hi @bep, shouldnt it trigger? quote : it triggers the update event in the listeners of all the nodes from the node that is being updated to the top.;
So what is the correct way to listen for update?
@arqex I like the your "observable" solution compared to flux, flux is way more complicated
from freezer.
hi @bep, shouldnt it trigger?
Maybe ... but it, in the example, you set b
, I guess you're replacing it ... and then the listener is gone ... ? My point is, a workaround is to pull the listener one level up.
from freezer.
Sorry @bep I forgot to reply to your last comment.
When you set a child, you are replacing the child node by a new one, so the listener of the node is gone too.
You can also reset the node, instead of replacing it and the listener should be kept
store.get().a.b.reset([1,2,4]);
http://jsbin.com/sojupatogo/1/edit?js,console
When a change is made, the update
event is triggered in all the parents, so you could listen to changes in all those nodes. But I would highly recomend not to add listeners in the nodes and isolate partial rendering depending on those events.
Fine grained rendering makes your app much more complex and difficult to understand, soon you will find yourself asking why is some part of the app not being updated after a change. You should listen to the top node events and re-render your whole app when any change happens, you will be amazed how performant is react's diff algorithm.
If you think that the performance is not good enough, it is much easier to declaratively stop re-rendering some part when the props don't change (à la pureRenderMixin since react data is immutable this is really easy) than imperatively re-render the part of the store that are changing.
from freezer.
Clear answer
from freezer.
Related Issues (20)
- Trigger returns undefined HOT 1
- multiple parents and node invariant check HOT 4
- Cannot sort arrays HOT 4
- always run in transact() mode HOT 4
- bug: nodes of value 0 get affected while updating its empty-array sibling HOT 1
- Typescript definition file HOT 4
- Store doesn't reset between launches. HOT 2
- state not updating when it should HOT 1
- Handle high frequency updates HOT 1
- JavaScript heap out of memory when using freezer-js HOT 2
- MS Edge compatibility issue with processQueue() HOT 6
- Set function is not working HOT 2
- Freezer: Can't add node to the tree. It's already added and freezer is configured to `singleParent: true`
- Object References and relations across state changes? HOT 1
- Bower version not updated HOT 2
- Best way to implement Provider / connect( ... ) for React components using freezer? HOT 2
- Parents reference of some children not updated after reset HOT 1
- Pivot on whole freezer object? HOT 3
- Interactive blog posts and documentation HOT 1
- Provide a warning when attempting to update the state of an outdated freezer object HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from freezer.