GlideAjax: can we still do "initialize" variables?

tahnalos
Kilo Sage

So I realize that using an initialize function on a GlideAjax is a bad thing to do.

On regular script includes, I use the initialize function to declare certain variables that will be used by the class.  Is there any way to do this on a GlideAjax script include if the initialize function cannot be used?

Thanks 

1 ACCEPTED SOLUTION

Allen Andreas
Administrator
Administrator

Hi,

Additional example in this article here: https://community.servicenow.com/community?id=community_article&sys_id=1f0f6af11be7cd14c552c8031d4bc...

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

View solution in original post

7 REPLIES 7

Jace Benson
Mega Sage

Yes, however you need to add some extra bits to make the Client Callable part still work. 

I have this in my "template" when I make a GlideAjax.

var SomeUtil = Class.create();
SomeUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    // If you want to use initialize you can only if you include
    // AbstractAjaxProcessor with something like this;
    /*
    initialize: function(request, responseXML, gc) {
        global.AbstractAjaxProcessor.prototype.initialize.call(this, request, responseXML, gc);
        // Your code
    },
    */
    awesomeFunction: function(){
        var inputObj = JSON.parse(this.getParameter('sysparm_obj'));
        var returnObj = {
            from:"server",
            input: inputObj
        };
        return JSON.stringify(returnObj);
    },
    type: 'SomeUtil'
});

Client Callable script includes extend the AbstractAjaxProcessor, so overwriting any of the methods in there like initialize or getName will cause problems.

Are you suggesting that the initialize in your example needs to be declared as an extension of the AbstractAjaxProcessor initialize function?  And this will allow for this.<variableName> declarations?

Thanks

I'm suggesting that if you want a method called initialize and you want this to still work you'll need to also add that additional line to it.

Allen Andreas
Administrator
Administrator

Hi,

Additional example in this article here: https://community.servicenow.com/community?id=community_article&sys_id=1f0f6af11be7cd14c552c8031d4bc...

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!