Yomguithereal / baobab

JavaScript & TypeScript persistent and optionally immutable data tree with cursors.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Listening to elements in Array not working with strings

psypersky opened this issue · comments

commented

Hi there, when listening to elements in arrays everything works OK when selecting the element with ['foo', 0] and it does not works when selecting with foo.0, is this how it should work?

I'm having problems with Cerebral.js since cerebral parses everything into a string, by the polymorphism of the selectors it looks to me that it should work that way but I'm not totally sure.

var Baobab = require("baobab");
var tree = new Baobab({
    foo: [
        { id: 0, val: 'zero' },
        { id: 1, val: 'one' },
    ]
});
tree.select(['foo', 0]).on('update', () => console.log('zero updated 1'));
tree.select('foo.0').on('update', () => console.log('zero updated 2'));
tree.merge(['foo', { id: 0 }], { val: 'zzeerroo' });

Here is the test in Runkit:
https://runkit.com/58c9b468b035460013583d34/58e2fe1ee08ac000145eff6f

Thanks for the cool library 🥇

Hello @psypersky. Baobab does not support string sugar for selection like your example foo.0 to avoid having to parse strings and deal with escaping etc. (I know weird people who use dots in their properties' names...). So, your result is to be expected.

commented

I see, yeah that makes sense, then this is just a cerebral problem.

Thank you very much for the help! 😄