WP8 上的 Cordova:requirejs 无法解析视图
Cordova on WP8: requirejs can't resolve views
我有一个使用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;
}
- 如何使用javascript从主svg对象动态创建svg视图框
- 正在使用$location.path(.)路由ng视图
- angular.js没有'无法在PhoneGap中处理视图标记
- 如何包含特定于每个视图angularjs的javascript文件
- 通过在Dojo mobile ViewController.openExternalView中动态更改打开同一外部视图的
- Kendo:我该如何在树视图中创建一个递归的hieiarchy
- Ajax Live搜索发布到Laravel视图
- backbone.js无法渲染视图
- 根据某些条件在视图之间切换
- ng视图外的链接重定向到ng视图内的页面
- 以可优化的方式使用requirejs加载模板
- RequireJS向模块传递配置总是返回undefined
- Backbone+RequireJS+Mediator模式导致视图逻辑短路和无限循环
- 为RequireJS构建AngularJS视图
- Durandal:在找不到视图/视图模型时处理 RequireJS 错误
- WP8 上的 Cordova:requirejs 无法解析视图
- RequireJS, Pub/Sub.如果没有糟糕的解决方法,我无法从其自己的方法访问视图实例
- 如何使用requireJs加载一个动态视图css文件骨干
- RequireJS + Backbone:导入视图模块会与模型模块发生冲突
- 使用 Knockout.js / RequireJs 将 javascript “类”或“模块”与视图模型分离