WP8 上的 Cordova:requirejs 无法解析视图

Cordova on WP8: requirejs can't resolve views

本文关键字:视图 requirejs 上的 Cordova WP8      更新时间:2023-09-26

我有一个使用requirejs加载其模块的Web应用程序。Web应用程序在任何桌面浏览器中都可以正常工作,当与Cordova打包时,它也可以在iOS和Android上运行。但是,在构建Windows Phone 8 Cordova应用程序时不起作用。

我收到以下错误:

"未找到视图:通过路径"text!views/shell.html搜索"views/shell",以搜索

(我正在使用杜兰达尔)

我有以下应用程序结构:

  • 库/要求/要求.js
  • www/app/viewmodels/shell.js
  • www/app/views/shell.html
  • www/app/main.js
  • www/index.html(包含行:)

www/app/main.js 包含以下代码:

requirejs.config({
    //baseUrl: 'x-wmapp0:www/app',
    baseUrl: 'app',
    enforceDefine: true,
    paths: {
        'text': '../lib/require/text',
        'durandal':'../lib/durandal/js',
        'plugins' : '../lib/durandal/js/plugins',
        'transitions' : '../lib/durandal/js/transitions',
        'knockout': '../lib/knockout/knockout-2.3.0',
        'bootstrap': '../lib/bootstrap3/js/bootstrap',
        'jquery': '../lib/jquery/jquery-1.9.1',
        'modules' : 'modules'
    },
    shim: {
        'bootstrap': {
            deps: ['jquery'],
            exports: 'jQuery'
       }
    }
});
define(['durandal/system', 'durandal/app', 'durandal/viewLocator', 'bootstrap'],  function (system, app, viewLocator, bootstrap) {
    //>>excludeStart("build", true);
    system.debug(true);
    //>>excludeEnd("build");
    app.title = 'MyApp';
    app.configurePlugins({
        router: true,
        dialog: true,
        widget: true
    });
    app.start().then(function() {
        //Replace 'viewmodels' in the moduleId with 'views' to locate the view.
        //Look for partial views in a 'views' folder in the root.
        viewLocator.useConvention('viewmodels', 'views', 'views');
        //Show the app by setting the root view model for our application with a transition.
        app.setRoot('viewmodels/shell', 'entrance', 'applicationHost');
    });
});

此代码在除WP8以外的所有设备上都能完美运行。我尝试了 baseUrl 行:"x-wmapp0:www/app" 来设置 WP8 Cordova 内部使用的实际路径,但这不起作用。那是因为它不是以"/"或 http 开头的路径吗?

关于如何配置 requirejs 以便能够使用 requirejs 加载模块和视图的任何想法?

看看这对你是否有任何帮助 http://mikaelkoskinen.net/durandal-phonegap-windows-phone-8-tutorial/

此外,截至2013年10月18日,Phonegap Build增加了对WP8的测试版支持。添加"winphone:作为gap:platform,并确保您使用的是PhoneGap 3.0。我们目前没有运气,但也许你可以使用它。

我也为自己解决了这个问题,我最终所做的是在加载main.js之前等待deviceloaded事件。

Cordova对XMLHttpRequest应用了一个补丁,该补丁在触发deviceloaded时应用。

在我的应用程序中,它最终看起来像这样:

<script src="js/require.js"></script>
<script>
document.addEventListener('deviceloaded', function() {
  var script = document.createElement('script');
  script.src = 'js/main.js';
  document.body.appendChild(script);
}, false);
</script>

我希望这对你也有用!

另一件事,我在我的应用程序中注意到,如果我尝试加载weinre,我根本无法使用require.js启动我的应用程序。不过,我还没有能够进一步研究这个问题。

我建议将断点设置为cordovalib/XHRHelper.cs HandleCommand方法,看看发生了什么。此外,以下版本的HandleCommand可以更好地为您工作

    public bool HandleCommand(string commandStr)
    {
        if (commandStr.IndexOf("XHRLOCAL") == 0)
        {
            string url = commandStr.Replace("XHRLOCAL/", "");
            Uri uri = new Uri(url, UriKind.RelativeOrAbsolute);
            try
            {
                using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (isoFile.FileExists(uri.AbsolutePath))
                    {
                        using (
                            TextReader reader =
                                new StreamReader(isoFile.OpenFile(uri.AbsolutePath, FileMode.Open, FileAccess.Read))
                            )
                        {
                            string text = reader.ReadToEnd();
                            Browser.InvokeScript("__onXHRLocalCallback", new string[] { "200", text });
                            return true;
                        }
                    }
                }
                url = uri.AbsolutePath;
            }
            catch { }
            var resource = Application.GetResourceStream(new Uri(url, UriKind.Relative)) ??
                // for relative paths and app resources we need to add www folder manually
                // there are many related issues on stackoverflow + some prev worked sample don't because of this
                Application.GetResourceStream(new Uri("www/" + url, UriKind.Relative));
            if (resource == null)
            {
                // 404 ? 
                Browser.InvokeScript("__onXHRLocalCallback", new string[] { "404" });
                return true;
            }
            else
            {
                using (StreamReader streamReader = new StreamReader(resource.Stream))
                {
                    string text = streamReader.ReadToEnd();
                    Browser.InvokeScript("__onXHRLocalCallback", new string[] { "200", text });
                    return true;
                }
            }
        }
        return false;
    }