javascript - How to avoid Meteor helpers run multiple times -


in template have html:

<input id="name"   type="text" value="{{card.name}}"> <input id="prefix" type="text" value="{{card.prefix}}"> <input id="phone"  type="tel"  value="{{card.phone}}"> 

and javascript

template.cardform.helpers({   card: function() {     return getcard();   } });  var getcard = function() {     console.log("i'm here !!!");     return cards.findone({_id: cardid}); }  

when run app, console.log shows "i'm here !!!" 3 times, , think ecards.findone() executing 3 times.

how can avoid calls?

i want card object in order fill {{card.name}}, {{card.prefix}} , {{card.phone}}, 1 call getcard().

the card helper executed several times since card present in evaluated template code more once.

a pattern avoid duplicate calls in scenarios 1 you're facing use #with:

{{#with card}}   <input id="name"   type="text" value="{{name}}">   <input id="prefix" type="text" value="{{prefix}}">   <input id="phone"  type="tel"  value="{{phone}}"> {{/with}} 

this call card once , run nested code in context of result.


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -