MVCoconut / coconut.data

Observable Data Layer.
The Unlicense
20 stars 8 forks source link

Model persistence examples? #49

Closed cambiata closed 4 years ago

cambiata commented 5 years ago

It would be great with an example or two how to best deal with model persistence, at least from and to json data.

kevinresol commented 4 years ago

The simplest model would be something like this:

class Persistence<Data> implements Model {
    @:constant var load:()->Promise<Data>;
    @:constant var save:Data->Promise<Noise>;
    @:observable private var revision:Int = 0;
    @:loaded var data:Data = {revision; load();}

    @:transition
    function save(data:Data) {
        return save(data).swap({revision: revision + 1});
        // note: you can also use some local cache mechanism to avoid the reload
    }

    @:transition
    function refresh() {
        return {revision: revision + 1}
    }
}
cambiata commented 4 years ago

Thanks, very helpful!

Here's my take on implementing the whole chain, if anyone's interested: https://gist.github.com/cambiata/0298d909ae151f638cb1be4dbfc60392

kevinresol commented 4 years ago

Closing for now. One day we will move this to the website.