简体   繁体   English

使用jQuery Mobile和PhoneGap加载动态JavaScript / HTML

[英]Loading dynamic JavaScript/HTML with jQuery Mobile & PhoneGap

I'm developing a mobile app using Dreamweaver CS6 (with PhoneGap ), jQuery Mobile and JavaScript that will collect user-inputted form data, transform the data into a JSON object and transmit it to a python web service. 我正在开发使用移动应用Dreamweaver CS6 (带PhoneGap ), jQuery Mobile和JavaScript将收集用户输入的形式数据,将数据转换成JSON对象,并将其发送到一个python web服务。 The purpose of the mobile app is to let users create a food order which is then sent to the restaurant. 移动应用程序的目的是让用户创建食物订单,然后将其发送到餐厅。 I wanted to inquire with the SO community on what would be an elegant way of handling the logic of creating the order. 我想向SO社区询问什么是一种优雅的方式来处理创建订单的逻辑。 Currently, the logic is defined by the JavaScript in the HTML page, example below (truncated for brevity): 当前,逻辑由HTML页面中的JavaScript定义,下面的示例(为简洁起见被截断):

JavaScript: JavaScript:

    var burgerType; //defines burger type eg.cheeseburger,hamburger
    var burgerQueue = new Array(); //holds completed burgers

    //detects if cheeseburger
    $("#cheeseburger").click(function() {
        burgerType="cheeseburger";
    });

    //detects if hamburger
    $("#hamburger").click(function() {
        burgerType="hamburger";
    });

HTML: HTML:

  <div data-role="content">
    <ul data-role="listview" data-theme="e" id="burgers">
      <li id="cheeseburger"><a href="#toppings">Cheeseburger</a></li>
      <li id="hamburger"><a href="#toppings">Hamburger</a></li>
    </ul>
  </div>

Screenshot: Clicking on a burger takes you to a screen that let's you choose the toppings , didn't include it because don't think it's relevant. 屏幕截图:单击汉堡会带您进入一个屏幕,让您选择浇头 ,但未包含该浇头 ,因为认为不相关。

I realize this is probably a very poor way of handing the logic because if the restaurant menu changes I would have to modify the app, republish it to their respective markets (Play,iTunes, etc), and have users update it. 我意识到这可能是交给逻辑,因为如果餐厅的菜单的改变,我将不得不修改应用程序,它发布到各自的市场(播放时,iTunes等),并让用户更新它的一个非常贫困的方式。 The last thing I'd wanna do is frustrate users by having to constantly update the app due to bugs or changes. 我想做的最后一件事是由于错误或更改而不得不不断更新应用程序,从而使用户感到沮丧。 I know I could partially separate the logic out of the HTML by using an external javascript library residing in the application directory, but that doesn't help with the constant update issue. 我知道我可以使用位于应用程序目录中的外部javascript库将逻辑部分地从HTML中分离出来,但这对持续更新问题没有帮助。 I would've made the app link directly to a mobile app page on a web server (so the changes I make are immediately seen without updating the app) but I read on another SO post the Apple gatekeepers don't publish apps that are basically just a webpage wrapped in an app. 我会做了直接的应用程序链接到移动应用页面的Web服务器上(所以我所做的更改会立即看到不更新应用程序),但我读了另一个SO发布苹果守门不发布基本应用只是包装在应用中的网页。 I'm looking at the django web framework and thinking it may be what I want, but not sure which is why I'm inquiring with the coding gurus here at SO to get ideas on what I should be doing. 我在看Django的web框架和思考它可能是我想要的东西,但不知道这是为什么,我与编码大师在这里SO询问得到什么我应该做的想法。

To recap: trying to dynamically generate a user-interface and coding logic using jQuery/JavaScript without frustrating users with constant updates. 回顾一下:尝试使用jQuery / JavaScript动态生成用户界面和编码逻辑,而不会因不断更新而使用户沮丧。 (Kinda like how jQuery let's you hotlink to their CDN-hosted libraries). (Kinda就像jQuery让您热链接到其CDN托管的库一样)。 I realize this might be confusing, I'll clarify any questions asked. 我意识到这可能会造成混淆,我将澄清所有提出的问题。

对于这种情况,最好使用像jQuery mobile这样的AJAX导航框架,该框架可以在不重新加载任何内容的情况下在页面之间移动,而只有一个真正的html文档。

You can bind a single event to multiple elements: 您可以将单个事件绑定到多个元素:

$("#burgers").on("click", "a" function() {
    var li = $(this).parent();
    burgerType = li.attr("id");

    $.mobile.changePage("#toppings");
    return false;
});

Then you just need to generate the list elements from data, probably based on an ajax call. 然后,您仅需要根据数据(可能基于ajax调用)生成列表元素即可。 For that sort of thing I usually make the ajax call in the background on app startup, and have the previous result of that call saved so that connecting for updates doesn't slow down app startup. 对于这种情况,我通常在应用程序启动时在后台进行ajax调用,并保存该调用的先前结果,以便连接更新不会减慢应用程序启动的速度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM