antonmedv/monkberry

Get state from view

dosper7 opened this issue ยท 6 comments

Hi @Elfet , I've been trying this framework and it seems great, and with it's small size and benchmarks it's sounds like the "next JQuery must use in any site" framework.

But a was trying to make an ajax request and get to state (just like when update the view we just call view.update(someObject) it seams that (for now) it's doesn't has a way to get that state, for enable me, for instance make a request to the server.

I was expecting something like knockout observables, when you update something the render engine does it magic but at anytime I can get that model (in this case a view) and work with is data the generated the current view/html/template.

Am I missing something? Is this feature present or do you plan do give support? Or can you give some hint of how can I do this with this framework?

Hi, thanks!

First of all Monkberry is not framework, it's library. So you can you is in any framework you want, or build your own.

If you need to update state from server you can do something like this:

fetch('https://...')
.then(response => response.json())
.then(state => view.update(state));

I'm going to write a bunch of articles about integrating Monkberry with frameworks like redux and other.

sorry for the mistake :) That's not what I wanted to ask.

What i wanted was something like this:

var view = Monkberry.render(Template, document.body);
view.update({name: 'World'});
var data = view.getState();
$.post('url',data);```

No, this is not possible. This is main feature of Monkberry โ€” it does not contains state, it's like pure function f(state) => view which will give you a view from state.

But get track of state it's pretty easily by your self:

function update(state) {
  Object.assign(currentState, state);
  view.update(currenctState);
}

update({name: 'World'});
$.post('url', currenctState);

Thanks ๐Ÿ‘ But it would be great if the API had this type of helpers instead of doing some custom extra work.

I think a dedicated monkberry-redux project would be pretty helpful, which I'm sure @Elfet is working on. But I'm inclined to agree with @Elfet โ€“ Monkberry should be kept pure by simply rendering view from some state. It allows for a huge amount of flexibility. It does come at the expense of out-of-the-box features but clearly that's not the aim of the library anyway.