jQTouch Notes
Last week I finally got to play with jQTouch, a jQuery based framework for building mobile-ready web sites. It was... rough to say the least. I'm very happy with the final result, but it took a bit of work to figure out how jQTouch wanted to work and what I should be doing with it. What follows are some basic notes, discoveries, and tips. I warn you though - most likely some of what follows could be a misunderstanding on my part.
1) First and foremost, jQTouch is really meant for web applications that are menu driven. When designing your application, you want to think in terms of menus and sections. Your primary UI will be menus and forms.
2) jQTouch works by convention. By using a button with a certain class, you build a back button. By using a link, you implicitly create an Ajax request. None of this was very clear to me at all.
3) The demos, for the most part, work around the idea of one file. The file contains all the menus and "pages" for the application. I don't think this is very realistic. A simple application can do this, but anything dynamic and of intermediate complexity will want separate files.
4) Given the above, here is a very basic, but complete template.
2<head>
3<script src="js/jquery.js"></script>
4<script src="js/jqtouch.min.js" type="application/x-javascript" charset="utf-8"></script>
5<style type="text/css" media="screen">@import "css/jqtouch.min.css";</style>
6<style type="text/css" media="screen">@import "themes/jqt/theme.min.css";</style>
7
8<script>
9$.jQTouch({
10 icon: 'jqtouch.png',
11 statusBar: 'black-translucent'
12});
13 </script>
14</head>
15
16<body>
17
18 <div id="home">
19 <div class="toolbar"><h1>Your App</h1></div>
20
21 <ul class="rounded">
22 <li class="arrow"><a href="#about">About</a></li>
23 <li class="arrow"><a href="#unicorn">Unicorn</a></li>
24 </ul>
25 </div>
26
27 <div id="about">
28
29 <div class="toolbar">
30 <h1>About</h1>
31 <a class="button cancel" href="#">Return</a>
32 </div>
33
34 <p>
35 Hello World
36 </p>
37
38 </div>
39
40 <div id="unicorn">
41
42 <div class="toolbar">
43 <h1>Unicorn</h1>
44 <a class="button cancel" href="#">Return</a>
45 </div>
46
47 <p>
48 Hello, Unicorns
49 </p>
50
51 </div>
52
53</body>
54</html>
From top to bottom, here is what you need:
- Need to load jQuery and the jQTouch JS library.
- Need to load the JQTouch CSS, and one theme file. jQTouch ships with two themes.
- You need to startup jQTouch. There are many initialization options (detailed here).
- Now for the important part. Your "home" DIV is your home page. It consists of a toolbar and a set of links. Those classes are recognized by jQTouch and help generate the UI. Notice the links! They are both anchor links. This means they are expected to be within the file itself.
- Scrolling down, you see two blocks that match up with the anchor links above. Once again each one has a toolbar.
That's basically it for a simple app. Here is a screen shot of the home page:

And here is a shot of one of the "pages":

Because of the CSS conventions we used, that's all you have to do. Clicking the menu link loads the page. Clicking Return brings you back home. It just plain works.
5) So hey, notice how that page doesn't render terribly nicely? The toolbar is fine, but the actual page content ain't so hot. This is something that really bugged me. All of the demos focused on menus and forms, but never showed "simple" content in a nice display. I asked about this on the Google Group (by the way, another big tip is to use the jQTouch Google Group) and the suggestion was to add CSS. I'm kinda surprised this doesn't exist already. Here is the CSS I added for my application.
2.body {
3 -webkit-border-radius: 8px;
4 -webkit-box-shadow: rgba(0,0,0,.3) 1px 1px 3px;
5 padding: 10px 10px 10px 10px;
6 margin:10px;
7 background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#4c4d4e), to(#404142));
8 word-wrap:break-word;
9}
10
11.body a {
12 color: white;
13}
14
15.body p {
16 margin-bottom: 10px;
17}
18</style>
I then wrapped the paragraphs in my 2 "pages" with the div class="body" tags. The result is a better looking page (imho):
6) So, I mentioned earlier that by default, jQTouch expects your pages to be in the same page. I don't think that makes much sense for a dynamic application. Luckily it's easy enough to work around. Any link that is not an anchor link is automatically converted to an AJAX get request. Let's add a link to our menu:
2 <li class="arrow"><a href="#about">About</a></li>
3 <li class="arrow"><a href="#unicorn">Unicorn</a></li>
4 <li class="arrow"><a href="cylon.html">Cylon</a></li>
5</ul>
Next we add a cylon.html file. Remember this is loaded via Ajax. Therefore, we just need our toolbar and body divs.
2
3 <div class="toolbar">
4 <h1>Cylons</h1>
5 <a class="button cancel" href="#">Return</a>
6 </div>
7
8 <div class="body">
9 <p>
10 Remember the basic rule. If the person is ugly, they can't be a Cylon in disguise.
11 </p>
12 </div>
13
14</div>
7) Ok, so what if you don't want to use AJAX for the request? For example, I had a logout link and I needed it to reload the entire page. This blog entry by Tim Golen describes all the way links work. If you don't feel like clicking the link, just add rel="external" to your links.
8) So the fact that AJAX is used to load stuff brought up two interesting problems for me. The first problem was authentication. My application has a login screen and I present the jQTouch UI after you logged in. I did this with a simple location() call in onRequestStart. This breaks when the AJAX request calls a page and you've logged off. I don't have my solution yet - but what I'm going to do is detect the AJAX request (which is super easy) and output something jQTouch can pick up on. Speaking of that...
9) The second issue I had was trying to do crap when the page loaded. I built my pages with the assumption that I could just use $(document).ready. This kinda worked. Well, no, not really. Most likely it was something I did wrong. jQTouch provides a set of events that you can listen to for your application. The event you want to use is pageAnimationEnd. The example in their docs though assume a one page app and makes use of bind. I had to make use of live instead. Here is an example:
2 if (info.direction == 'in') {
In this example, somepage is the ID of the main div around the page. info.direction will either be in or out. I used "in" as a way of handling the page load and "out" as a way of handling the page ending. This was pretty useful for me as the page in question needed a JavaScript interval. So I started it up when the page loaded and killed it when the page ended.
That's all I have for now. I linked to the Google Group above (but here it is again) - I'll also point out the Wiki: http://code.google.com/p/jqtouch/w/list. It is a bit more fleshed out then I remember it from last time. I've pasted a zip of my simple demo above. It should work fine anywhere. For my testing I use Safari shrunk as small as possible. I've also tested my application on both my Nexus One and an iPod Touch, and in both it looks fantastic. So despide the "roughness" of the framework for me, I definitely think it's pretty darn cool! I hope these tips help, and if anyone wants to add some additional tips, be my guest.

Using jqtouch was what got me into doing things with base64 images.
--Dave
Good stuff - after you tweeted about it this weekend, I was definitely interested to see where you went with it. In fact, after you tweeted about it, I decided to look into some CSS animations (which jQTouch uses -- wow, that was a humbling few hours).
I played with jQTouch a while back and found it to be very cool as long as it did exactly what you wanted; I think , like you, when you need to change it up a bit, it suddenly becomes a bit wonky. I had a situation where I wanted to bind() to a link that loaded an AJAX page and have the page that it linked to show a "loading..." string prior to the AJAX actually having been loaded.
After like 4 hours, I couldn't get that to work at all. I ended up having to manually trigger the "touch" event... and found out that manually triggering it didn't seem to actually doing anything...
Anyway, I'm feeling very inspired to see you working on this. Thanks!
<cf_jqtouch>
<cf_jqtouchhome>
<cf_jqtouchmenu label="About" src="about">
</cf_jqtouchome>
<cf_jqtouchpage id="about">
This is the abouve
</cf_jqtouchpage>
</cf_jqtouch>
**can't post url. flagged as spam.
add this to the url.
/blog/2010/06/17/introducing-sencha-touch-html5-framework-for-mobile/
First, there is a minor error in your example code. The primary div in the cylon.html has the same id as an already exiting div. Since these need to be unique this causes an issue if you click on the cylon menu item again.
Along with this it would appear that the href of the cylon menu item is rewritten to the id of the returning div after first click. The fact that the href is rewritten this would mean that it will not make subsequent calls to the server for new content. I have not yet found a way around this yet as it appears to happen due to jQtouch functionality.
Thanks,
--Dave
The content also gets the same div id unless it is dynamically changed. I am not sure what this might do to the app after continued, same session, use. It would be cool if the previously loaded content could be destroyed.
--Dave
http://9-bits.com/post/723711597/jqtouch-and-sench...
He created jQTouch first, which is of course powered by jQuery and MIT licensed. Similar to jQuery the main idea is to provide progressive enhancement to mobile browsers as we often do on normal websites for desktop browsers.
Sencha (previously Ext) hired him to build Sencha Touch. Similar to Ext JS the main idea is to build JavaScript-based applications for mobile devices.
So jQTouch hasn't been "replaced" by Sencha Touch by any means, they're separate libraries with slightly different use cases.
$('#somepage').live('pageAnimationEnd', function(<script type="text/javascript" src="http://remotesite.com/js/somescript.js">&l...;){
if (info.direction == 'in');
});
Where inside the #somepage div I have a <ul> menu, of which one of the <li> points to mypage.html -
Or:
$('folder/another/mypage.html').live('pageAnimationEnd', function(<script type="text/javascript" src="http://remotesite.com/js/somescript.js">&l...;){
if (info.direction == 'in');
});
None of these actually work, by the way, and my syntax is crap, I'm sure, but hopefully my question is clear enough for some guidance?
By the way, to muddy the waters further - when I do just put that <script>javascript</script> inside the </head> section of the main page, the javascript fires just fine on the remote pages using iOS devices. On Android, though, they break - so I'm looking for a cross-browser workaround.
I understand I'm kind of taking a tangent from your post/blog purpose, so really - any thoughtful assistance you can provide is so appreciated.
$("#somepage").live("pageAnimationEnd"), function(
alert('hi ray');
);