mantrajs / mantra-sample-blog-app

A sample blog app built with Mantra
http://mantra-sample-blog-app.herokuapp.com/
MIT License
296 stars 104 forks source link

Using Meteor Methods to get data when reactivity not needed. #69

Closed rnarayan closed 8 years ago

rnarayan commented 8 years ago

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

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.

ShockiTV commented 8 years ago

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.

arunoda commented 8 years ago

@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
rnarayan commented 8 years ago

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