Getting started

ranbena edited this page Sep 28, 2011 · 27 revisions

Basic Setup:

  1. Download the project files: zip, tar or fork it.
  2. Browse to index.html from your desktop and mobile.
  3. Edit the file to your needs.

If you have a server that runs php, start with index.php so you could enjoy Touchy™PHP which enables embedding assets into the page (potentially speeding up load time).

In the future we may add support for some other server side languages.

Explore Features

There's a whole lot you can do with the Touchy™ projects. Full documentation can be found in Touchy™JS and Touchy™PHP.

Before starting, make sure touchy.js is loaded into the page after jQuery or Zepto. The minified versions are lighter and faster.

<script src="js/libs/zepto.min.js"></script>
<script src="js/libs/touchy.min.js"></script>

Here's a few quick ones:

Fixed header

Your HTML structure should look like this:

<html>
  <head>
    <style>
      .touchy-js{display: none}; /* To avoid a flash of content */
    </style>
  </head>
  <body>
    <!-- header -->
    <div class="touchyjs-header">
      ...
    </div>

    <!-- content page #1 -->
    <div class="touchyjs-content" id="page1">
      ...
    </div>

    <!-- content page #2 -->
    <div class="touchyjs-content" id="page2">
      ...
    </div>
  </body>
</html>

Reload the page. Now, only the first "page" is displayed. Insert content into "page", refresh and drag the content up and down. The header bar stays static while the content scrolls.

Page Navigation

You can use TouchyJS.Nav to control navigation and handle url changes. Make sure you provide id attributes to the .touchyjs-content elements.

Insert a link in the first page, that executes TouchyJS.Nav.goTo("page id"). Like so:

<!-- content page #1 -->
<div class="touchyjs-content" id="page1">
  <a href="" onclick="TouchyJS.Nav.goTo('page2'); return false;">Go to next page</a>
  ...
</div>

Don't forget to add return false to avoid page redirection.

Side note: Urge you to put the event handlers in an unobtrusive script, not inline, but for the sake of this example...

Let's put a back button in the header:

<!-- header -->
<div class="touchyjs-header">
  <a href="" onclick="TouchyJS.Nav.back(); return false;" class="btn-back">Back</a>
</div>

Click it, and the first page comes into view.

Of course, TouchyJS.Nav.goTo('page1') would have worked just as well, but "back/forward" browser button functionality would have been lost.

The only problem now is that the back button is visible even in the first page. No good. We want it to appear upon navigation and disappear when returning. TouchyJS.Nav can handle that with it's attachCallback method. Quick code:

<script>
  var button = $("btn-back");
  
  // Show button when navigating out of the first page
  TouchyJS.Nav.attachCallback("page1", "out", function(){
    button.show();
  });
  
  // Hide it when returning
  TouchyJS.Nav.attachCallback("page1", "in", function(){
    button.hide();
  });
</script>

TouchyJS.Nav is even smarter than that, letting you attach functions to url address change events. More on that in the full documentation.

Environment Info

Getting device info is valuable for cross-platform cross-browser web-apps. TouchyJS.Env lets you tap into environment info by CSS hooks as well as by javascript API.

CSS hooks will look like this:

<html class="iphone webkit touch mobile orientation-portrait ">
  <head>...</head>
  <body>...</body>
</html>

And you can use javascript as well like this:

<script>
  var info = TouchyJS.Env.getInfo();
  var platform = info.platform.name;
  var browser = info.browser.name;
  var isTouch = TouchyJS.Env.isTouch();    
  var isMobile = TouchyJS.Env.isMobile();  
</script>

Get Geocoded Location

(This feature is not active yet, but will be shortly)

Another exciting feature of TouchyJS.Env is getLocation.

Say you have a location aware web-app that uses the browser's navigator.geolocation. You might want to notify the user of the location you detected, but position coordinates are not user friendly.

Try this instead:

<script>
  TouchyJS.Env.getLocation(function(data){
    alert("You're in "+data.city+", "+data.country);
  });
</script>

Again, the alert is for this example only, do not use alerts - users hate it.

The callback will return the argument data containing the following:

  • city
  • country
  • state
  • zip
  • position

Be aware that not all data parameters may be present. For instance, Kabul in Afghanistan returns no zipcode and any point in Antarctica returns nothing but position coordinates.

If the device running your app doesn't have GPS capability - no worries, it'll fallback to retrieving the data by the device's IP address.

More on that in the full documentation.

TouchyJS.Events

Right now, there's one relevant function to the TouchyJS.Events object - attaching a callback to TouchyJS.Events.ready. Then, TouchyJS and all of its objects and methods are ready for use.

TouchyJS.Events.ready(func);

Example use:

TouchyJS.Events.ready(function(){
    // Get searchbar value
    TouchyJs.Searchbar.getValue();
});

<tml:searchbar>

Almost every web-app utilizes a searchbar. <tml:searchbar> provides the following functionality without the hassle:

  • Placeholder text
  • Keyboard integration - "Search" key label and submit on press
  • Clear button (coming soon)

Here's how to use it:

<tml:searchbar onsubmit="searchFunc"></tml:searchbar>

More on <tml:searchbar> in the full documentation.

<tml:image>

Need to resize your images on the fly? <tml:image> serves your image in any dimensions you specify.

Here's how to use it:

<tml:image src="image.png" width="100" height="50"></tml:image>

More on <tml:image> in the full documentation.

Stand by for more <tml> tags coming very soon.