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

toJS() func not working anymore in freezer-js@0.11.0 #84

Closed daoquangphuong closed 7 years ago

daoquangphuong commented 8 years ago

Please check toJS func in freezer-js@0.11.0

arqex commented 8 years ago

Hi @daoquangphuong

Seems to work for me

http://jsbin.com/mubucizope/1/edit?js,console

What's exactly failing?

Cheers, Javi

daoquangphuong commented 8 years ago

ok. i have one "socket.io-client@1.4.5", with event:

var store = new Freezer({})

socket_client.on('updateStore', function (data) {
    // add clone data in this line
    var state = store.get();
    Object.keys(data).forEach(function (name) {
        state = state.set(name, data[name]);
    })
});

store.on('update',function(){
       console.log(store.get().toJS()) // <- working
       console.log(store.get().a.toJS()) // <- toJS func is undefined in here
})

and i emit from server:

socket_server.emit('updateStore',{a:{b:'c'}})

///// working normal in my chrome browser. but not working in node-webkit v 0.12.3 base on Chromium 41.0.2272.76

if i downgrade to freezer-js@0.10.0 -> woking normal or clone data before set in store: data = JSON.parse(JSON.stringify(data)) -> woking normal

arqex commented 8 years ago

I suspect that store.get().a is not a plain JS but an instance of some class.

Try to create your store like:

var store = new Freezer({},{freezeInstances: true});

That should parse class instances and add freezer methods to them.