How do I rewrite code which is using globals to AMD

JS globals are deprecated and due to be removed in one of the Jira 8.0.x releases . They are replaced with AMD modules.

In our code we extend global objects for example:

BoardFilter.SearcherCollection = JIRA.Issues.SearcherCollection.extend({
   initialize: function(models, options) {
      this.extendedFields = options.extendedFields;
      BoardFilter.SearcherCollection.__super__.initialize.apply(this, arguments);
   },

or

if ( typeof GH.VersionView.draw === 'function'){
     var versionShow = GH.VersionView.draw;
     GH.VersionView.draw = function(){
         versionShow.apply(this, arguments);

DEPRECATED: The global object GH.VersionController is deprecated. Please use require(["jira-agile/rapid/ui/version/version-controller"]) instead.

How do we do the same using AMD?

Cheers
Przemek

1 Like
require(["jira-agile/rapid/ui/version/version-controller"], function(versionController) {
    console.log(versionController)
});

or

var versionController = require("jira-agile/rapid/ui/version/version-controller");

@mashintsev

But…

Let’s say I need to modify the way scrum board works - for example rendering of versions on the version panel. We’re adding extra statuses there.
I extend global object and that way Atlassian code executes my code as well.

As far as I understand AMD, If I do

var versionController = require("jira-agile/rapid/ui/version/version-controller");

if ( typeof versionController.draw === 'function'){
     var versionDraw = versionController.draw;
     versionController.draw = function(){
         versionDraw.apply(this, arguments);
         .... my code....
    }
}

I won’t be able to overload the definition of “jira-agile/rapid/ui/version/version-controller” the same way global GH. object would do.

So, if the some backlog code will execute require(“jira-agile/rapid/ui/version/version-controller”).draw(), it will not execute “… my code…” as this is going to be a different object? Is that correct?

1 Like

Try out this method for overriding methods:

require(["undescore", "jira-agile/rapid/ui/version/version-controller"], function(_, versionController) {
 
    _.extend(versionController.prototype, {

        draw: function () {

        },
    });
});