arqex / freezer

A tree data structure that emits events on updates, even if the modification is triggered by one of the leaves, making it easier to think in a reactive way.
MIT License
1.28k stars 56 forks source link

set() and going "up a node" #37

Closed aktxyz closed 8 years ago

aktxyz commented 9 years ago

http://jsbin.com/jezoja/2/edit?js,console

When chaining the set() method, is there a way to "go back up the tree".

For example, these 2 behave different...

frozen .set('a', 'aaaaaa') .set('b', 'bbbbbb') .c.set('d', 'dddddd') ;

AND

frozen .set('a', 'aaaaaa') .c.set('d', 'dddddd') .set('b', 'bbbbbb') ;

It looks like this is because the set() returns the node in being acted on, and not the root node. Is there a way in the 2nd scenario above to go back up a node so the set('b') works as expected?

aktxyz commented 9 years ago

not pretty, and may have probably has issues, but this seems to work...

frozen .set('a', 'aaaaaa') .c.set('d', 'dddddd').__.parents[0] .set('b', 'bbbbbb') ;

arqex commented 9 years ago

Hi @aktxz,

Yes, that way you can access to the parent. Parent access is not recommended, because there must be more than one parent for a node, so you can break your app if you are not sure of what parent to update.

If your component has access to the parent node I would do

frozen.set({a: 'aaa', b: 'bbb'})
  .c.set({d: 'ddd'})

If your component has no access to the node, I would emit an event or pass a callback to it to notify the parent that needs to be updated.

arqex commented 8 years ago

Hi @aktxyz

It's been a long time, but there are news about this issue. Latest update of freezer comes with a method called pivot. Using it you can make your node updates return the pivoted node. In your example you could do:

frozen.pivot()
  .set('a', 'aaa')
  .c.set('d', 'ddd')
  set('b', 'bbb')
;

Once you pivoted frozen all the updates in the children return the updated frozen object.