Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get state from view #12

Open
dosper7 opened this issue Jul 4, 2016 · 6 comments
Open

Get state from view #12

dosper7 opened this issue Jul 4, 2016 · 6 comments
Labels

Comments

@dosper7
Copy link

dosper7 commented Jul 4, 2016

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
Copy link
Owner

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
Copy link
Owner

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

@dosper7
Copy link
Author

dosper7 commented Jul 4, 2016

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
Copy link
Owner

antonmedv commented Jul 4, 2016

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
Copy link
Author

dosper7 commented Jul 4, 2016

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

@benjamminf
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants