Code Monkey home page Code Monkey logo

meteor-reactive-method's Introduction

Meteor Reactive Methods

Call methods synchronously inside Tracker.autorun.


LAST UPDATED 10 APRIL 2019


Install with meteor add bartoftutormundi:reactive-method

Sometimes, you want to call a Meteor method inside of a template helper or Tracker.autorun computation, and get a return value. Now you can!

Before (doesn't work)

Template.foo.helpers({
   methodResult: function () {
       Meteor.call("myMethod", "a", "b", function (err, result) {
           return result; // this doesn't work!!!
       });
   }
});

After (works!)

Template.foo.helpers({
    methodResult: function () {
        // Super fun!
        return ReactiveMethod.call("myMethod", "a", "b");

        // Can also use 'apply' style
        // return ReactiveMethod.apply("myMethod", ["a", "b"]);
    }
});

Method results are updated every time their arguments change

Be careful! If you pass something like Random.id() or new Date() as one of the arguments, you will cause an infinite loop where the method is called infinitely over and over. If you don't want your method call to re-run on every argument change, try this other package: https://atmospherejs.com/mnmtanish/call

Invalidating method calls

Sometimes, you want to force a reactive method to get a new value from the server, even though the arguments are the same. In that case, use ReactiveMethod.invalidateCall or ReactiveMethod.invalidateApply, like so:

// Will cause the helper above to rerun
ReactiveMethod.invalidateCall("myMethod", "a", "b");

meteor-reactive-method's People

Contributors

dimda avatar ecwyne avatar namenotrequired avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.