0

I want to rerender a blockhelper when a session variable changes.

UI.registerHelper "myHelper", () ->
  this.changedSession = Session.get 'someSessionVariable'
  return Template.myTemplate

The helper is actually executed but the view is not updated. Ideally only the part that the helper renders would be rerendered (Template.myTemplate). But rerendering the complete page would also be an option.

How can I do this?

A very simple app that illustrates my problem:

The CoffeeScript:

if Meteor.isClient
  Template.hello.greeting = () ->
    Session.get "greeting"

UI.registerHelper "myHelper", (options) ->
  this['greeting'] = Session.get 'greeting'
  return Template.test

The HTML:

<template name="hello">
  {{greeting}}
  {{#myHelper test="test" more="more"}}

  {{/myHelper}}
</template>

<template name="test">
    {{this.greeting}}
</template>

If I change the session variable. The greeting itself is updated, the template containing the greeting is not updated.

I know this is not the meteor way to get the session in the helper, but I am using this for a prototyping framework and this helper can be used in any template without defining a data context for the helper.

Roland Studer
  • 4,315
  • 3
  • 27
  • 43

2 Answers2

1

This seems like the correct behavior. The view won't be updated unless it depends on the session variable itself. There is a way to work around this though. Instead of returning Template.myTemplate try returning this:

Template.myTemplate.extend({
  render: function () {
    // do something with your session here ...
    return Template.myTemplate.render.apply(this, arguments);
  }
});
Tomasz Lenarcik
  • 4,762
  • 17
  • 30
  • This now longer works with the newest version of meteor (0.8.3) it returns "undefined is not a function". So I guess UI.component has changed the behavior – Roland Studer Aug 04 '14 at 16:24
0

For Meteor 1.0 you can do the following:

In your blockhelper do:

UI.registerHelper "myHelper", (options) ->
  new Template Template.myTemplate.viewName, ->
    Session.get 'something' #do something reactive...
    return Template.myTemplate.renderFunction.apply this, arguments

If you actually know in which template the helper is used then it is probably better to use one of the possbilities described in https://stackoverflow.com/a/23450884/2037537. In my example I needed the block helper to work anywhere without knowing in which templates I would actually use it.

Community
  • 1
  • 1
Roland Studer
  • 4,315
  • 3
  • 27
  • 43