stubailo / meteor-reactive-method

Call methods synchronously inside Tracker.autorun [deprecated]
https://atmospherejs.com/simple/reactive-method
MIT License
98 stars 11 forks source link

Method rerunning even if args dont change #14

Closed patrickml closed 9 years ago

patrickml commented 9 years ago

For some reason my meteor methods are being called even if I dont update the params.

Look at this for example

HTML


 <tr>
        <td>
          Assets : {{money totalBalance.assets}}
        </td>
        <td>
          Liabilities : {{money totalBalance.liabilities}} =
        </td>
        <td>

        </td>
      </tr>

JS

totalBalance : function () {
    var result = ReactiveMethod.call("getBalance", 'a');
    return result || { assets : 0, liabilities : 0 };
  }

on the server I am seeing the following happen


I20150806-13:46:47.886(-4)? a <- Param
I20150806-13:46:48.233(-4)? test <- random consolelog
I20150806-13:46:48.233(-4)? a <-Param
I20150806-13:46:48.554(-4)? test <- random consolelog

Any Ideas?

stubailo commented 9 years ago

@patrickml I think your helper is actually called twice by Blaze.

You can get around this by using {{#with}}, or explicitly making a new variable to cache the result of the method.

Here's a hack to call the helper only once with {{#with}}:

{{#with totalBalance}}
 <tr>
        <td>
          Assets : {{money assets}}
        </td>
        <td>
          Liabilities : {{money liabilities}} =
        </td>
        <td>

        </td>
      </tr>
{{/with}}
patrickml commented 9 years ago

Though this works I still feel like if this package is caching the results it should return the result from the cache and not need to recall the method on the server unless the params change or the method is invalidated. Thank you for the awesome though it did work perfectly.

stubailo commented 9 years ago

@patrickml yeah... but who know how long it should cache it for? Like, if you remove the template and then open it again 2 minutes later, the results from the server are likely to be different. If you need complicated caching I would suggest manually using a ReactiveVar and a Tracker.autorun to call the method when you need to. This package is just meant to satisfy the simplest use case in a convenient way.

patrickml commented 9 years ago

Yeh I guess thats true thank you for you help!

stubailo commented 9 years ago

No problem, thanks for asking!