Handlebars 在 Phonegap Mobile 上不起作用
Handlebars Doesn't Work on Phonegap Mobile
我正在使用版本 0.4.2 构建 PhoneGap 应用程序。
我在我的代码中使用 Handlebars 模板,我使用这一行编译它:
var template = Handlebars.compile($("#myTemplateName").html());
该应用程序在桌面和移动浏览器上运行正常,但当我尝试使用 PhoneGap Developer App(版本 1.7.6)测试移动版本时,该行不起作用。控制台没有显示任何错误输出,但我认为应用程序一定是崩溃了,因为应用程序变得无响应并且该行代码之后的任何警报都不会显示。
我还尝试预编译车把模板并使用此行获取它们:
var template = Handlebars.templates["myTemplateName"];
我得到了相同的回复:该应用程序在浏览器中运行良好,但在移动设备上却神秘地停止运行。
为什么会这样?
其他可能有用的信息:
- 除了 PhoneGap,我还在使用 jQuery Mobile
- 我使用的是 Handlebars 4.0.5 版
- jQuery Mobile、Handlebars 和 Handlebars 模板包含在我的 index.html 脚本标签文件中(按此顺序)。
编辑: 根据要求,这里有一个例子:
<!DOCTYPE html>
<html>
<body id="body">
<!-- Visible Page -->
<div data-role="page" id="home">If this is visible it's not working.</div>
<!-- Handlebars Library -->
<script type="text/javascript" src="/js/handlebars-v4.0.5.js"></script>
<!-- Handlebars Template -->
<script id="template-content" type="text/x-handlebars-template">{{data}}</script>
<!-- jQuery Mobile -->
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<!-- PhoneGap -->
<script type="text/javascript" src="cordova.js"></script>
<script>
$( document ).ready(function() {
var myTemplate = Handlebars.compile($("#template-content").html());
$("#home").html(myTemplate({data:"It's working!"}));
});
</script>
</body>
在浏览器上我看到 "It's working" 但使用 PhoneGap Developer 移动应用我看到 "If this is visible it's not working."
Phonegap 可能正在将 <script type="text/javascript" src="/js/handlebars-v4.0.5.js"></script>
更改为 <script type="text/javascript" src="file://js/handlebars-v4.0.5.js"></script>
改为<script type="text/javascript" src="js/handlebars-v4.0.5.js"></script>
还要确保 jQuery 正常工作,因为您使用的是 CDN,Phonegap 可能不会下载它
我正在使用版本 0.4.2 构建 PhoneGap 应用程序。
我在我的代码中使用 Handlebars 模板,我使用这一行编译它:
var template = Handlebars.compile($("#myTemplateName").html());
该应用程序在桌面和移动浏览器上运行正常,但当我尝试使用 PhoneGap Developer App(版本 1.7.6)测试移动版本时,该行不起作用。控制台没有显示任何错误输出,但我认为应用程序一定是崩溃了,因为应用程序变得无响应并且该行代码之后的任何警报都不会显示。
我还尝试预编译车把模板并使用此行获取它们:
var template = Handlebars.templates["myTemplateName"];
我得到了相同的回复:该应用程序在浏览器中运行良好,但在移动设备上却神秘地停止运行。
为什么会这样?
其他可能有用的信息:
- 除了 PhoneGap,我还在使用 jQuery Mobile
- 我使用的是 Handlebars 4.0.5 版
- jQuery Mobile、Handlebars 和 Handlebars 模板包含在我的 index.html 脚本标签文件中(按此顺序)。
编辑: 根据要求,这里有一个例子:
<!DOCTYPE html>
<html>
<body id="body">
<!-- Visible Page -->
<div data-role="page" id="home">If this is visible it's not working.</div>
<!-- Handlebars Library -->
<script type="text/javascript" src="/js/handlebars-v4.0.5.js"></script>
<!-- Handlebars Template -->
<script id="template-content" type="text/x-handlebars-template">{{data}}</script>
<!-- jQuery Mobile -->
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<!-- PhoneGap -->
<script type="text/javascript" src="cordova.js"></script>
<script>
$( document ).ready(function() {
var myTemplate = Handlebars.compile($("#template-content").html());
$("#home").html(myTemplate({data:"It's working!"}));
});
</script>
</body>
在浏览器上我看到 "It's working" 但使用 PhoneGap Developer 移动应用我看到 "If this is visible it's not working."
Phonegap 可能正在将 <script type="text/javascript" src="/js/handlebars-v4.0.5.js"></script>
更改为 <script type="text/javascript" src="file://js/handlebars-v4.0.5.js"></script>
改为<script type="text/javascript" src="js/handlebars-v4.0.5.js"></script>
还要确保 jQuery 正常工作,因为您使用的是 CDN,Phonegap 可能不会下载它