opened
rasdaniil
username_0
So here is an idea and a situation which I ran into and had to dirty patch the mobx.js file as I'm not sure how to arrange it in typescript..

**1.**
Let's say we have a class.. this is a simplified example

```
class TestModel {
    constructor (paramsObj) {
        this._someInternalProp = '';
        this._someInternalProp2 = '';
        this._fields = observable ( paramsObj )
    }

    toJS() {
       return toJS(this._fields);
    }
}

let model1 = new TestModel({param1: 1});
let model2 = new TestModel(param1: 2);
```

Now we set a map of TestModels:

```
let testMap = observable.map( );
testMap.set(id1, model1);
testMap.set(id2, model2);
```



Now we want to get serialised representation of this map:

`let js = toJS(textMap)
`

Result:

```
{ id1: {
    _someInternalProp... // Not what we want
...
```


Now if we add the following code to mobx.js:568 ...

```
+    else if(source.toJS && typeof source.toJS == "function")
+        return source.toJS();
 
      return source;
```

let js = toJS(textMap)

// {id1 : { param1: 1},  id2: {param2: 2}}



**2.**
Another application where this could be useful is some sort of seamless observable.map() wrapper. I'm writing a firebase-to-mobx-map adapter and wanted it to repeat all methods of map (values, entries etc) by passing them onto a private internal observable.map, unfortunately toJS has to be implemented as a method and called separately

```
class FirebaseMap {
 _collection = observable.map();

// lot's of other stuff, internal props and methods etc

toJS() { return toJS( this._collection); } 
entries() { return this._collection.entries() }
values() { return this._collection.values() }
// etc..

}

toJS( new FirebaseMap() ) // returns all internal stuff
```



Hope I explained more or less clear..
Custom toJS serialisation support
issue
created
rasdaniil
username_0
Also I need to say that the library is really enjoyable to work with, thanks for great work to the author and all contributors
comment
created
mweststrate
username_1
Didn't really read the issue so far, so this response might not make sense :) But did you check the serializr package?
comment
created
rasdaniil
username_0
Yes I definitely opened the page, and absolutely sure the package works great, just didn't want to add whole new package to the project (and of course to study it would take some time).

The idea (shortly speaking) is to add possibility to feed toJS() func with custom behaviour when it bumps into our object, which for different reasons could need custom behaviour. For example to hide private fields

Maybe I'm overcomplicating and it could be done easier 

Thanks for reply
comment
created
JabX
username_2
I needed to support a custom behaviour into the `toJS` function and I ended up copying it in my project and add some stuff here and there. The function is pretty short so I suppose it would be an acceptable solution.
comment
created
mweststrate
username_1
@username_0 yeah I think it is a good idea. However i'm not entirely sure about the `toJS` name. Maybe it is safer to introduce a symbol for that, like `Mobx.toJS` to avoid conflicts?
comment
created
mattruby
username_3
Possibly belongs in mobx-utils
comment
created
rasdaniil
username_0
That would definitely make it very handy in some situations, gives more control over things
as for the name, you should be right I suppose
thanks for the work
comment
closed
mweststrate
username_1
issue
created
mweststrate
username_1
Closing. For full control on serialization there is already serializr and mobx-state-tree. No need to introduce a third way.
comment