NvdB31
NvdB31

Reputation: 81

How to modify object property before insertion into Blaze template

Let's say I have the following Blaze template helper that fetches some objects from a collection:

PackageInCart: function() {
            PackageIds = Carts.findOne({SessionId: SessionID}).Packages;
            var PackageObjects = Packages.find({ _id: { $in : PackageIds } } ).fetch();         
            return PackageObjects;
    },

The PackageObjects variable contains objects that have a 'priceperday' property with a certain price value. In the Blaze template, I can easily print this value using:

{{#each PackageInCart}}
 <div class="price">{{priceperday}}</div>
{{/each}}

However, what if I want to modify the 'priceperday' value from the Helper function before it gets printed in the template? What would be the correct way to do this?

One solution that came to mind was to make a for loop that iterates over the objects and does something like Object.defineProperty() to change the priceperday property into the new value.

I want to know if there's an easier or quicker way using Blaze methods to modify the object property that gets printed with the curly braces.

Upvotes: 0

Views: 110

Answers (1)

Jan Jouke Tjalsma
Jan Jouke Tjalsma

Reputation: 610

If you want to do this using blaze you could do this using another helper.

weeklyPrice: function(priceperday){
  return priceperday * 7;
}

Which would be called like this

{{#each PackageInCart}}
  <div class="price">{{weeklyPrice priceperday}}</div>
{{/each}}

More info about spacebars helper arguments in the docs

Upvotes: 1

Related Questions