首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Jira Gadget - Config屏幕上的Reload AJAX

Jira Gadget - Config屏幕上的Reload AJAX
EN

Stack Overflow用户
提问于 2012-05-17 11:05:06
回答 1查看 1.7K关注 0票数 2

在任何地方都找不到解决办法,我真的希望这是可能的。

我正在编写一个Jira小工具,我有一个配置屏幕,有两个字段。其中一个是quickfind项目选择器;您键入并找到项目,然后单击所需的项目。

第二个字段是组件。您可以选择要筛选的项目的组件。但是,每个项目的组件是不同的,因此组件字段使用在"config“部件的"args”部分中的小工具中指定的AJAX调用填充。

唯一的问题是,这个AJAX只在小工具第一次加载时才会被调用;也就是说:在选择项目之前,结果总是"Select“。

我需要一种方法来在所选项目被更改的事件上重新运行这个AJAX调用。

这个是可能的吗?还是有另一种解决方案?我尝试了计时器来检查更改,但也有一些问题;主要是无法访问/更改组件拖放框字段。这个小玩意会拒绝装载。

更新:下面是这个小工具的Javascript。如您所见,我添加了一个refreshComponents() Javascript方法,它可以检索给定项目ID的组件,但是我无法将它附加到适当的事件。而且,我似乎不能用jQuery或普通JS直接修改页面上的任何组件

代码语言:javascript
复制
<div id="chart_div" style="overflow: auto;"></div>

        <script type="text/javascript">

                var gadget = this;
                google.load('visualization', '1.0', {'packages':['corechart']});

                var oldProject = "initiated";
                var globalGadget;
                function timedComponentUpdate()
                {
                    //alert(globalGadget.getPref("projectId"));
                    //setTimeout("timedComponentUpdate()",3000);
                }

                function refreshComponents(idString)
                {
                    //refetch components
                    var url = "__ATLASSIAN_BASE_URL__/rest/severity-gadget/1.0/severity-issues/getComponents.json";
                    url += "?projectId=" + idString; 
                    alert(url);

                    var xmlhttp;
                    if (window.XMLHttpRequest)
                        xmlhttp=new XMLHttpRequest();
                    else
                    {
                        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                    }
                    xmlhttp.onreadystatechange=function()
                        {
                            if (xmlhttp.readyState==4)
                            {
                                //document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
                                alert(xmlhttp.responseText);
                            }
                        }

                    xmlhttp.open("GET",url,true);
                    xmlhttp.send();
                }

                function drawChart(args, bugtype, comp) {
                    //setup for whether were getting opened or closed
                    var axisTitle;
                    var compTitle;
                    var chart;

                    if(bugtype == "Bug")
                        axisTitle = "Bug";
                    else
                        axisTitle = "Issue";

                    if(comp == "All")
                        compTitle = "";
                    else
                        compTitle = " - Component: " + comp;

                    var wVar = gadgets.window.getViewportDimensions().width-20;
                    var hVar = wVar/3;
                    var hVar = hVar*2;

                    // Create the data table.
                    var data = new google.visualization.DataTable();
                    data.addColumn('string', 'Issues');
                    data.addColumn('number', 'Trivial');
                    data.addColumn('number', 'Minor');
                    data.addColumn('number', 'Major');
                    data.addColumn('number', 'Critical');
                    data.addColumn('number', 'Blocker');

                    AJS.$(args.weeks).each(function() {
                        data.addRow(["Week "+this.number,
                            parseInt(this.issues[0]),
                            parseInt(this.issues[1]),
                            parseInt(this.issues[2]),
                            parseInt(this.issues[3]),
                            parseInt(this.issues[4])
                        ]);
                    });

                    var options = {'title':'Weekly '+axisTitle+' Backlog' + compTitle,
                          'width':wVar,
                          'height':hVar,
                           axisFontSize:4,
                           isStacked:true,
                           fontName: '"Arial"'
                    };

                    chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
                    chart.draw(data, options);
                }

                var gadget = AJS.Gadget(
                    {
                        baseUrl: "__ATLASSIAN_BASE_URL__",
                        useOauth: "/rest/gadget/1.0/currentUser",
                        config: {
                            descriptor: function(args)
                            {
                                document.getElementById("chart_div").innerHTML = "";

                                var gadget = this;
                                var projectPicker = AJS.gadget.fields.projectOrFilterPicker(gadget, "projectId", args.projectOptions);

                                //bh
                                oldProject = this.getPref("projectId");
                                //refreshComponents(this.getPref("projectId"));

                                return {
                                    theme : function()
                                    {
                                        if (gadgets.window.getViewportDimensions().width < 450)
                                        {
                                            return "gdt top-label";
                                        }
                                        else
                                        {
                                            return "gdt";
                                        }
                                    }(),
                                    fields: [
                                        AJS.gadget.fields.nowConfigured(),
                                        projectPicker,
                                        AJS.gadget.fields.days(gadget, "weeksPrevious"),
                                        {
                                            userpref: "issueType",
                                            label: "Issue Type:",
                                            description:"Choose which issue type you would like",
                                            type: "select",
                                            selected: this.getPref("issueType"),
                                            options:[
                                                {
                                                    label:"Any",
                                                    value:"Any"
                                                },
                                                {
                                                    label:"Bug",
                                                    value:"Bug"
                                                }
                                            ]
                                        },
                                        {
                                            userpref: "component",
                                            label: "Component:",
                                            description:"Choose which issue type you would like",
                                            type: "select",
                                            selected: this.getPref("component"),
                                            options:args.components
                                        }
                                    ]
                                };
                            },
                            args: function()
                            {
                                return [
                                {
                                    key: "components",
                                    ajaxOptions: function() {
                                        var ajaxProject = this.getPref("projectId");
                                        if(ajaxProject == "")
                                            ajaxProject = "null";

                                        return {
                                            url: "/rest/severity-gadget/1.0/severity-issues/getComponents.json",
                                            data:
                                            {
                                                projectId : ajaxProject
                                            }
                                        }
                                    }

                                }

                                ];
                            }()
                        },
                        view: {
                            onResizeReload: true,
                            onResizeAdjustHeight: true,
                            template: function(args) {
                                var gadget = this;

                                gadget.getView().empty();

                                drawChart(args.issueData, this.getPref("issueType"), this.getPref("component"));

                                gadget.resize();
                            },
                            args: [{
                                key: "issueData",
                                ajaxOptions: function() {
                                    return {
                                         url: "/rest/severity-gadget/1.0/severity-issues.json",
                                         data:  {
                                            projectId : gadgets.util.unescapeString(this.getPref("projectId")),
                                            weeksPrevious: this.getPref("weeksPrevious"),
                                            issueType: this.getPref("issueType"),
                                            component: this.getPref("component"),
                                            backlog: true
                                        }
                                    };
                                }
                            }]
                        }
                    }
                );
        </script>
EN

回答 1

Stack Overflow用户

发布于 2016-04-19 09:59:39

我认为您需要将组件字段转换为回调生成器

在回调函数中,您需要做一些事情:

  1. 通过AJAX请求检索选项
  2. 渲染下拉
  3. 附加事件处理程序以在发生特定事件时刷新列表

您的新组件字段可能看起来像这样..。我假设您已经为简洁提供了jQuery。

代码语言:javascript
复制
{
    userpref: "component",
    label: "Component",
    id: "component_field_id"
    description: "Choose which issue type you would like",
    type: "callbackBuilder",
    callback: function(parentDiv){

        function renderOptions(options){
            // Remove elements from the parentDiv and replace them
            // with new elements based on the options param
            // You can use gadget.getPref('component') to ensure you
            // mark the right option as selected
        }

        function getOptions(){
            $.ajax({
                 url: "__ATLASSIAN_BASE_URL__/rest/severity-gadget/1.0/severity-issues/getComponents.json",
                 data: {
                     // You might be able to get hold of the selected value
                     // from the gadget object instead of like this
                     projectId: $("#filter_projectOrFilterId_id").val() 
                 }
            }).done(renderOptions);
        }

        // Retrieve and render options on gadget load
        getOptions();

        // Retrieve and render options on an event
        $(parentDiv).on("update-options", getOptions);
    }
}

此外,当项目选择字段值发生更改时,需要触发一个事件。在JS代码中的其他地方(而不是在小工具定义中),您需要像这样放置代码,但是您需要确认项目/过滤器选择器的CSS选择器:

代码语言:javascript
复制
$(document).on("change", "#filter_projectOrFilterId_id", function(){
    $("#component_field_id").trigger("update-options");
});

我还没有测试过这个,但这就是我要达到你要求的目的的方法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10634507

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档