Server script runs twice with AngularJS directive on service portal widget

Zach22
Tera Contributor

I am working on a service portal widget. I want the initial visibility of the widget/button to depend on the result of a server-side call. I've created a function, c.onLoad() that accomplishes this. If I add onLoad="c.onLoad()" to the element, the client and server script run only once as they should.

But if I instead use ng-init="c.onLoad()" or some other AngualrJS directive that calls the onLoad function, the client script appears to run once but the server script runs twice! I was able to verify this with the client and server logs.

 

I don't see why using the angular directive causes the server script to run twice? Maybe somehow the controller is running twice?

 

find_real_file.png

HTML 

<div style="padding-bottom:5px;">
<button id="cancel" type="button" class="btn btn-danger btn-block" ng-init="c.onLoad()" >Cancel</button>
</div>

 

Client

function (spModal) {
    /* widget controller */
    var c = this;

    c.onLoad = function () {
        console.log("I only run once");
        c.server.get({
            ...
        }).then(function (r) {
            ...
        });
    }
}

 

Server

(function () {
    gs.log("CHCEK: I should not run twice");
})();
1 ACCEPTED SOLUTION

Thom8
Giga Expert

Just e thought: Is it because you execute the service side function IIFE and then with the c.server.get you trigger server side again?

View solution in original post

3 REPLIES 3

Thom8
Giga Expert

Just e thought: Is it because you execute the service side function IIFE and then with the c.server.get you trigger server side again?

Zach22
Tera Contributor

Maybe but here's what's interesting: I just commented out the entire client script and the server script is still running when the page loads for some reason. Maybe this isn't angular related?

Zach22
Tera Contributor

Oh yeah, I guess that may be the case. But is there any way to prevent the script from running on load? SN seems to encourage this format as the default?