mantrajs / mantra-sample-blog-app

A sample blog app built with Mantra

Home Page: http://mantra-sample-blog-app.herokuapp.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Using Meteor Methods to get data when reactivity not needed.

rnarayan opened this issue · comments

commented

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:

  1. Do I still need "useDeps" in app/containers/post_list.js ?
  2. 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);

commented

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.

  1. You need to do useDeps because, you need to get Meteor from the context. Not the one available globally.
  2. 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
commented

Thanks @xcv58 , @ShockiTV and @arunoda for your replies. @arunoda, I will use that cache package that you mentioned. Thanks for the recommendation.