简体   繁体   中英

Calling a javascript function defined in KRL from outside a KRL closure

I'm defining a Javascript function in my KRL global block that I want to call when the user clicks a link. Here are the relevant parts of the ruleset:

global {
  emit <|
    function clear_hold() {
      app = KOBJ.get_application("a421x26");
      app.raiseEvent("clear_hold");
    }

  |>;
}

rule add_link_to_clear_hold {
  select when pageview ".*"
  pre {
    clear_div = << <div id="clear_hold">
      <a href="javascript:clear_hold()">Clear Hold</a>
      </div> >>;
  }
  {
    append("body", clear_div);
  }

rule clear_the_hold {
  select when web clear_hold
  {
    replace_html("#clear_link", "<div id='clear_link'>Not on hold</div>");
  }
  always {
    clear ent:hold;
  }
}

When I click the link I get an error message that clear_link is not defined.

What do I need to do to call my javascript function?

It is suggested to use the following name spacing method to attach JavaScript functions to the KOBJ object to avoid clashes with other apps the user might have running.

KOBJ.a60x33.clear_hold = function() { 
  KOBJ.log('...wohoo! You found me!'); 
}

The function can then be called with

KOBJ.a60x33.clear_hold();

The function is defined inside the KRL closure, but I was calling from outside the closure. To make it visible outside I added it to KOBJ after defining the function

KOBJ.clear_hold = clear_hold;

Then to call it from the link:

href="javascript:KOBJ.clear_hold()

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