简体   繁体   English

JIRA中显示问题时如何运行JavaScript代码

[英]How to run JavaScript Code when an issue is displayed in JIRA

I am currently developing a JIRA 7 server add-on, I am stuck and I'd be grateful for any help I can get. 我目前正在开发JIRA 7服务器附加组件,遇到了麻烦,如果能获得任何帮助,我将不胜感激。 I am new to both JIRA and JavaScript, so forgive me if I fail to see the obvious solution :-) 我是JIRA和JavaScript的新手,所以如果我看不到明显的解决方案,请原谅我:-)

My Add-On includes a web panel which needs some JavaScript to function. 我的附加组件包括一个Web面板,该面板需要一些JavaScript才能起作用。 I intend the JavaScript to register a click handler that triggers a dialog. 我打算让JavaScript注册一个触发对话框的单击处理程序。 Currently it looks something like this: 当前看起来像这样:

AJS.toInit(function() {

  var buttonClickHandler = function() {
    AJS.dialog2("#myDialogId").show();
  };

  AJS.$("#myButtonId").click(buttonClickHandler);
}

The actual ids of the dialog and the button depend on the issue which is displayed. 对话框和按钮的实际ID取决于显示的问题。 Obiously I want this code to run when my dialog2 and my button are present in the DOM, which is everytime that I navigate to an issue (not just the first time I navigate to any issue). 令人讨厌的是,当我的dialog2和按钮出现在DOM中时,我希望此代码能够运行,这是每次我导航到一个问题时(不仅仅是我第一次导航到任何问题时)。 Unfortunately I do not know how to do that. 不幸的是,我不知道该怎么做。 I have tried the following approaches, neither of which worked: 我尝试了以下方法,但都没有用:

  1. bind my JavaScript file to the atl.general context. 将我的JavaScript文件绑定到atl.general上下文。 If I do that the script runs the first time I load a page that belongs to the atl.general context, ie the dashboard, any issue, etc., but when I navigate to another issue the script does not run again. 如果这样做,脚本将在第一次运行时加载属于atl.general上下文的页面,即仪表板,任何问题等,但是当我导航至另一个问题时,脚本不会再次运行。

  2. bind my JavaScript file to a custom context and use that in my template. 将我的JavaScript文件绑定到自定义上下文,然后在我的模板中使用它。 If I do that the script runs the first time that an issue is displayed which has that web panel, but not run again, when I navigate to a different issue. 如果执行此操作,则该脚本在第一次显示具有该Web面板的问题时运行,但是当我导航到另一个问题时,该脚本不会再次运行。

I figured I could find a solution based on jira events, ie register a function as an event handler using JIRA.bind() and put my code in there. 我认为可以找到基于jira事件的解决方案,即使用JIRA.bind()将函数注册为事件处理程序,然后将代码放入其中。 However that would require an event that is triggered whenever I want my code to run, ie whenever an issue is displayed. 但是,这将需要一个事件,只要我希望我的代码运行,即每当显示问题时,都将触发该事件。 I tried JIRA.Events.NEW_CONTENT_ADDED, function(e, context, reason) {...}) , but the event handler was never called (I checked with console.log() statements). 我尝试了JIRA.Events.NEW_CONTENT_ADDED, function(e, context, reason) {...}) ,但是从未调用过事件处理程序(我使用console.log()语句进行了检查)。 I have no idea which other Jira events exist which I might use to my advantage, nor can I find any documentation on it. 我不知道还有哪些其他的Jira事件可以利用,这对我也没有好处,我也找不到关于它的任何文档。

If you are building the button & the dialog2 HTML yourself, then you can control the HTML and JavaScript yourself. 如果您自己构建按钮和dialog2 HTML,则可以自己控制HTML和JavaScript。

Use a button which has a data-controls attribute to tell it which dialog2 to trigger: 使用具有data-controls属性的按钮来告诉它触发哪个dialog2:

<button class="aui-button your-button-class" data-controls="#your-dialog-uniqueId">Show dialog</button>

<!-- Render the dialog -->
<section role="dialog" id="your-dialog-uniqueId" class="aui-layer aui-dialog2 aui-dialog2-medium" aria-hidden="true">
   ...
</section>

...then you can add JavaScript which reads the ID of the dialog2 element from the data-controls attribute in the button and trigger the appropriate dialog2: ...然后,您可以添加JavaScript,该JavaScript从按钮中的data-controls属性读取dialog2元素的ID并触发相应的dialog2:

AJS.toInit(function($) {    
  $(".aui-button.your-button-class").on('click', function() {
    var dialogId = $(this).data('controls');
    AJS.dialog2(dialogId).show();
  });
});

Well, I could not find out how to run my script every time that an issue is displayed, but I did find a practical solution to my problem: 好吧,每次出现问题时,我都找不到运行脚本的方法,但确实找到了解决问题的实用方法:

Instead of registering the click handler to the button direcly, I registered it to the topmost DOM-Element that I knew would not be removed as long as I don't leave the context which my script is bound to: the body tag with the id "jira". 我没有将点击处理程序直接注册到按钮,而是将其注册到了我知道不会被删除的最顶层DOM元素,只要我不离开脚本绑定到的上下文即可:id为body的标签“吉拉”。 Here is how: 方法如下:

AJS.$("#jira").on("click", "#myButtonId", buttonClickHandler);

This will apply to any button with the id "myButtonId" inside the element with id "jira". 这将应用于ID为“ jira”的元素内ID为“ myButtonId”的任何按钮。 Even if the former hasn't been inserted yet. 即使前者尚未插入。 See jQuery documentation for details. 有关详细信息,请参见jQuery 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Jira 问题收集器代码在嵌入 html 但不在单独的 javascript 文件中时有效 - Jira issue collector code works when embedded in html but not in separate javascript file 当我运行此JavaScript代码以打印数组时没有显示任何内容。此代码有什么问题? - Nothing is displayed when I run this JavaScript code to print an array.What is wrong with this code? 代码在 Javascript 中运行的时间/频率 - When/how often does code run in Javascript Javascript-如果div已经显示,不运行代码吗? - Javascript - Don't run code if the div is already displayed? 无法通过javaScript中的jira rest api在jira中创建问题 - unable to create issue in jira through jira rest api in javaScript 页面上显示的Javascript代码 - Javascript code displayed on page 如何使用JavaScript在Jira 5.1.8的“发行链接”弹出窗口中隐藏某些发行链接类型? - How to hide some issue link types in the Issue Link pop up window for Jira 5.1.8 using javascript? 仅在定义变量时如何在JavaScript中运行代码? - How to run code in JavaScript, only when a variable is defined? 调用回调时如何在javascript中运行php代码? - How to run php code in javascript when callback is called? 如何在重定向到其他站点时运行javascript代码? - How can I run a javascript code when redirecting to another site ?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM