0

I am building an an application that has a Cart. The Cart has an attribute called cartPrice which gets updated when a user adds or removes items from the cart. I want the client to call a server method to return the updated value of cartPrice whenever the user adds/removes an item and the cartPrice changes. Here is my code:

cart.js:

Template.Cart.helpers({
   cartPrice: function() {
     return ReactiveMethod.call("returnCartPrice");
   }
});

cart.html:

<p>Cart Total: ${{cartPrice}}</p>

server side method to return cart price:

returnCartPrice: function(error, result) {
    var currUser = Meteor.user();
    var result = currUser.cartPrice.toFixed(2);
    return result;
}

The problem I have is the price on the client side will only update if I refresh the page.

Can someone help?

Thank you.

Trung Tran
  • 13,141
  • 42
  • 113
  • 200
  • See [this question](https://stackoverflow.com/questions/28640400/get-meteor-method-return-value-inside-helper-function) for how to call a method from a helper. You need a reactive variable to change in order for your helper to run again. I'd need to know how the cart is stored in order to give a more precise answer (is it in a collection / session variable, etc). – David Weldon Jun 12 '15 at 02:56
  • Thanks David. I added the reactive-variable package and now the cart price is able to update. However, it will only update on the client side if i refresh the page... I updated my question to show the server side code as well as reactive-variable code... – Trung Tran Jun 12 '15 at 03:07
  • Oh, so the only place the cart info is stored is on the user's document? If so, you don't even need a method for this. – David Weldon Jun 12 '15 at 03:23

0 Answers0