Why is WInJS included automatically when targeting Windows 8 in Cordova?

梦想的初衷 提交于 2019-12-13 01:31:30

问题


We're developing an app using AngularJS, and when we're targeting Windows 8 I noticed that the generated Visual Studio project included WinJS as a reference. Since we're not using WinJS I simply removed the reference from the project.

Then I noticed that removing WinJS caused benign script load errors in the console when running the app. Further investigation showed that cordova.js automatically checks for WinJS, and if not included tries to include it (!). Here's the relevant code:

var onWinJSReady = function () {
    var app = WinJS.Application;
    var checkpointHandler = function checkpointHandler() {
        cordova.fireDocumentEvent('pause',null,true);
    };

    var resumingHandler = function resumingHandler() {
        cordova.fireDocumentEvent('resume',null,true);
    };

    app.addEventListener("checkpoint", checkpointHandler);
    Windows.UI.WebUI.WebUIApplication.addEventListener("resuming", resumingHandler, false);
    app.start();
};

if (!window.WinJS) {
    var scriptElem = document.createElement("script");

    if (navigator.appVersion.indexOf("Windows Phone 8.1;") !== -1) {
        // windows phone 8.1 + Mobile IE 11
        scriptElem.src = "//Microsoft.Phone.WinJS.2.1/js/base.js";
    } else if (navigator.appVersion.indexOf("MSAppHost/2.0;") !== -1) {
        // windows 8.1 + IE 11
        scriptElem.src = "//Microsoft.WinJS.2.0/js/base.js";
    } else {
        // windows 8.0 + IE 10
        scriptElem.src = "//Microsoft.WinJS.1.0/js/base.js";
    }
    scriptElem.addEventListener("load", onWinJSReady);
    document.head.appendChild(scriptElem);
}
else {
    onWinJSReady();
}

I guess my main question is, should I just leave the WinJS reference "as is" and let Cordova load and initialize WinJS?

Could it potentially conflict with AngularJS or reduce the performance of the app in any way? (I guess var app = WinJS.Application and app.start() in onWinJSReady makes me a bit worried).

Since the app seems to work fine without the WinJS script files, why is cordova.js so insistent on trying to include it?


回答1:


cordova (and some cordova plugins, particularly the FileSystem plugin) use some features of WinJS, such as Promises and Ajax calls. We ended up forking cordova.js and stripping out all the WinJS stuff... makes the app load a lot faster!



来源:https://stackoverflow.com/questions/28233531/why-is-winjs-included-automatically-when-targeting-windows-8-in-cordova

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!