Using Meteor Methods to get data when reactivity not needed.
rnarayan opened this issue · comments
Arunoda, First of all, tons of thanks for mantrajs. Its superb.
I am starting a new project and I have decided to use Mantra for this. I don't care for reactivity in all situations. For a non-reactive solution, would you recommend the code below? It has worked in my tests.
My Questions:
- Do I still need "useDeps" in app/containers/post_list.js ?
- Let's say, user moves away from postlist page to post page, does "compose" call "componentWillUnmount" to destroy the postlist data? What's the best way to make sure that the data persists in the client and when the user comes back to the postList page from post-page, data is not fetched from the server again. Thanks a lot in advance.
import PostList from '../components/postlist.jsx';
import {useDeps, compose, composeAll} from 'mantra-core';
function composer(props, onData) {
Meteor.call('posts', function (error, posts) {
if (posts) onData(null, {posts});
})
};
export default composeAll(
compose(composer),
useDeps()
)(PostList);
I'm not sure what exactly you asked. But for
destroy the postlist data
I always use the SubsManager to subscribe all publish. It works great for me.
You dont need useDeps nor composeAll.
If you return function from composer, it will be executed when component is umounting, so you can do cleanup there.
That method call data is i internal variable, so it will get destroyed. You can call action to fetch it to for example ReactiveDict as in blog example is error, but you can use array there to store results.
So it will be available even if you umount.
@rnarayan Let me answer you questions.
- You need to do useDeps because, you need to get Meteor from the context. Not the one available globally.
- With methods, there's no default way to do it. For subscriptions, we've subs manager. For methods, you need to build a your own caching machanism. You could use this project: https://www.npmjs.com/package/lru-cache