How to link local (not global) JSView helpers and then accessing programmatically

47 Views Asked by At

I have some local helpers I am passing into the link method. One of the helpers is a filter function that depends on a helper variable. How do I access the local helpers so I can set the filter depends on the variable? I'm guessing I need to access the specific view after the link is done and then set the depends. Not sure how to access those local helpers though (as in myHelpers) directly after the link call is done.

In laymans terms I'm looking for something like:

tmpl.view.helpers.myHelpersFilter.depends = '~myHelpersParam'

jsview link template

1

There are 1 best solutions below

0
BorisMoore On

UPDATED:

A simple approach is to set the depends directly on the filter function, myHelpersParam, before passing it into the tmpl.link() call:

var myHelpers = {
  myHelpersFilter: function(...) {...},
  myHelpersParam: ...
};

myHelpers.myHelpersFilter.depends = '~myHelpersParam';

tmpl.link("#container", data, myHelpers)

Alternatively, a more flexible and sophisticated approach is to add an onBind event to the tag which is doing the filtering:

{^{for lineItems filter=~myHelpersFilter onBind=~myOnBindEvent ...}}

and set the depends on the filter in that event handler:

var myHelpers = {
  myHelpersFilter: function(...) {...},
  myHelpersParam: ...,
  ...
  myOnBindEvent: function (tagCtx, linkCtx, ctx, ...) {
    ctx.myHelpersFilter.depends = '~myHelpersParam'; // Set the depends for the filter
    this.baseApply(arguments); // Call base onBind() handler
  },
};