2013-10-17 35 views
2

我有一个使用requirejs加载其模块的Web应用程序。 Web应用程序在任何桌面浏览器中均可正常工作,与Cordova打包在一起时,它也适用于iOS和Android。然而,在构建Windows Phone 8 Cordova应用程序时不起作用。WP8上的Cordova:requirejs无法解析视图

我得到以下错误:

“查看未找到:搜索: “!意见/壳” 通过路径 “文本的意见/ shell.html”

(我使用迪朗达尔)

我有以下应用程序结构:

  • 的lib /要求/ require.js
  • WWW /应用程序/的ViewModels/shell.js
  • WWW /应用/视图/ shell.html
  • WWW /应用/ main.js
  • WWW/index.html中(包含行:)

WWW /应用/ 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加载模块和视图的任何想法?

回答

0

我只是为自己解决这个问题,我最终。做在等待deviceloaded事件之前我装我main.js

科尔多瓦applies a patch to XMLHttpRequestdeviceloaded已被触发已应用

在我的应用我它结束了寻找与此类似:

<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启动我的应用程序。尽管如此,我还没有能够进一步研究。

0

我建议设置一个断点到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; 
    }