antonmedv / monkberry

Monkberry is a JavaScript library for building web user interfaces
https://monkberry.js.org
MIT License
1.49k stars 78 forks source link

Get state from view #12

Open dosper7 opened 8 years ago

dosper7 commented 8 years ago

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?

antonmedv commented 8 years ago

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));
antonmedv commented 8 years ago

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

dosper7 commented 8 years ago

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);```
antonmedv commented 8 years ago

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);
dosper7 commented 8 years ago

Thanks :+1: But it would be great if the API had this type of helpers instead of doing some custom extra work.

benjamminf commented 8 years ago

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.