简体   繁体   中英

Meteor helper has run

How can I check all Meteor helpers have run?

When I use this code, I get a new, empty div. When I remove the code from the rendered function and run it from my console, everything works fine.

Template.CasesShow.helpers({
  value: function (n) {
    if (this.data) {
      var result = this.data.filter(function (obj) {
        return obj.name == n;
      });

      if (result && result[0]) 
        return result[0].value;
    }
  }
});

Template.CasesShow.rendered = function () {
  $(document).ready(function () {
    $textarea = $('[name=1]');

    var content = $textarea.val().replace(/\n/g, '<br />');

    $textarea.replaceWith($('<div class="box">' + content + '</div>'));
  });
};

<template name="CasesShow">
  <textarea class="w-input box" placeholder="{{_ 'laborauftrag.praxis'}}" name="1" data-name="1">{{value 1}}</textarea>
</template>

So I think, Meteor hasn't inserted the value yet, which is strange because it shouldn't run the rendered function then, right?

How can I make sure Meteor has run the helpers?

Template.rendered = func will run once before your template's helper (and long before your route provides you data). Your template isn't working when you have Template.rendered function because in your rendered function, you replace your textarea with div, and in helper you're returning value which is being set on the textarea which no longer exist (because Template.CaseShow.rendered has replaced it with <div> .

If you can provide more details about what you're actually trying to achieve here, we can solve that. What you have right now is intended behaviour of meteor.

If what you want to achieve is show your content in a div but after replacing /n with <br> , I believe you can do that by performing that regexp on your data in the template helper.

Put a console.log("FIRED VALUE HELPER"); and do the same for your .rendered console.log("TEMPLATE RENDERED"); The code will log in your client browser console. For chrome I right click on the browser and choose inspect element. Then choose console from the array of logs. Your client js code would look like this:

Template.CasesShow.helpers({
    value: function (n) {
       console.log("FIRED VALUE HELPER");


Template.CaseShow.rendered = function () {
  console.log("FIRED RENDERED");

If you don't see the log in the client browser console, the helper/rendered function did not get called.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM