user1733839
user1733839

Reputation: 13

WinJS.Namespace defined methods not accessible

I have a navigation app. In the page Control corresponding js files are present. I want to access an external js file which is in the project js folder. I have specified in

commonFunctions.js

WinJS.Namespace.define(

'commonFunctions', {

        authenticate: authenticate

    });

Now when accessing the method in the PageControl's js (page2.js) file

commonFunctions.authenticate();

it gives an error:

0x800a1391 - JavaScript runtime error: 'commonFunctions' is undefined

If there is a handler for this exception, the program may be safely continued.

Is this the right way to do this? Or am I missing something?

Upvotes: 0

Views: 1140

Answers (1)

Konstantin Dinev
Konstantin Dinev

Reputation: 34905

I believe the issue you're having is either that you have not referenced your script file inside the page you are loading or you have not wrapped your commonFunctions object in a function.

I tried this: script.js

(function () {
    WinJS.Namespace.define("CommonObject", {
        auth: function () {
            return true;
        }
    });
}());

default.js

    app.addEventListener("activated", function (args) {
        if (args.detail.kind === activation.ActivationKind.launch) {
            if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) {
                // TODO: This application has been newly launched. Initialize
                // your application here.
            } else {
                // TODO: This application has been reactivated from suspension.
                // Restore application state here.
            }

            if (app.sessionState.history) {
                nav.history = app.sessionState.history;
            }
            args.setPromise(WinJS.UI.processAll().then(function () {
                var test = CommonObject.auth(); // Comes out true and no exceptions
                if (nav.location) {
                    nav.history.current.initialPlaceholder = true;
                    return nav.navigate(nav.location, nav.state);
                } else {
                    return nav.navigate(Application.navigator.home);
                }
            }));
        }
    });

default.html

<script src="/js/script.js"></script>

Upvotes: 1

Related Questions