jQuery Mobile Essential Training

jQuery Mobile Essential Training

with Joe Marini

 


In this course, author Joe Marini uses the jQuery Mobile framework to create visually rich, interactive web pages for mobile devices. The course examines the construction of jQuery Mobile pages, showing how to build page transitions, dialogs, buttons, and toolbars. The course also delves into generating mobile-optimized web forms and list views and explains how jQuery Mobile handles events, including touch gestures and changes in device orientation.
Topics include:
  • Understanding jQuery Mobile page architecture
  • Including headers and footers
  • Creating navigation bars
  • Using text and search inputs
  • Understanding the basics of mobile forms
  • Working with radio buttons, check boxes, and toggles
  • Using collapsible content blocks and sets
  • Working with jQuery Mobile themes
  • Configuring the jQuery Mobile API

show more

author
Joe Marini
subject
Developer, Web, Mobile Web, Web Development
software
jQuery Mobile
level
Beginner
duration
3h 11m
released
Dec 09, 2011

Share this course

Ready to join? subscribe


Keep up with news, tips, and latest courses.

submit Course details submit clicked more info

Please wait...

Search the closed captioning text for this course by entering the keyword you’d like to search, or browse the closed captioning text by selecting the chapter name below and choosing the video title you’d like to review.



Introduction
Welcome
00:03Hi I'm Joe Marini and I would like to welcome you to jQuery Mobile
00:07Essential Training.
00:08In this title we will see how to use jQuery Mobile to build visually rich,
00:12engaging, and highly optimized mobile webpages that work well across a wide
00:17variety of today's modern smartphones and tablet devices.
00:20We will start off by examining how jQuery Mobile webpages are constructed and
00:24how the framework provides powerful enhancements such a smooth page transitions
00:28and pop-up dialogs that look almost like native applications.
00:32Next, we'll see how to use jQuery Mobile's built-in set of mobile-optimized
00:36user interface widgets and CSS3-based themeing architecture to build content,
00:42web forms, and list views that are optimized for mobile screen sizes and form factors.
00:47Then we will take a look at how jQuery Mobile provides support for handling
00:51common mobile events such as touch gestures and changes in device orientation
00:56just like native apps.
00:58If you're ready to build mobile web applications that work across a large number
01:02of popular devices without having to learn or write a single line of native
01:06application code then let's get started with jQuery Mobile Essential Training.
Collapse this transcript
What you should already know
00:00Before we get started building jQuery Mobile applications let's cover some of
00:04the things that you should already know.
00:06JQuery Mobile is a development framework for building mobile web applications,
00:11now that implies several things.
00:13First, since we are working with the web, you're going to need to know HTML and CSS.
00:18You are also going to need to know JavaScript, because applications implies
00:22logic and JavaScript is how you add logic to your web applications.
00:27So HTML, CSS, and JavaScript are some pretty fundamental technologies you're
00:32going to need to have a good understanding of in order to use this course.
00:36You should also be familiar with the fundamentals of mobile web development.
00:41You should be familiar with things like what viewports are and small screen
00:45size design and so on.
00:47The last thing you're going to need to know is jQuery itself, because JQuery
00:51Mobile is not a standalone library.
00:54It's actually built on top of the jQuery library and it requires the JQuery
00:59library in order to run.
01:00So you're going to need to be familiar with all the things you see here in the gray boxes.
01:05Now the good news is, if you aren't yet already familiar with the things in the
01:09gray boxes than there are plenty of great titles here on lynda.com where you
01:14can learn about this stuff and we'll take a look at those in a moment, but the
01:17idea here is that JQuery Mobile is the pinnacle here of some other technologies
01:21that you're going to need to have a good working knowledge of in order to use this course.
01:26So let's take a look at what you're going to need in order to build the
01:29applications we will be looking at here in this course.
01:32First, you're going to need the current version of the JQuery library itself.
01:37Now you can get the current version by going to jquery.com.
01:40I'm going to be using the 1.6.4 version in this course, but whatever the current
01:45version is by the time you get there it should be fine.
01:47You're also going to need the current version of the jQuery Mobile library,
01:51because as I mentioned before jQuery Mobile is built on top of jQuery and you're
01:55going to need both of them.
01:57Now you're also going to need a development tool like a text editor or an IDE.
02:00It doesn't matter which one you use.
02:02You can use your favorite for whatever platform you're using.
02:04You can use TextWrangler on the Mac or TextPad on Windows or commercial tool
02:09like Dreamweaver or free software tool like Aptana Studio.
02:12It doesn't really matter.
02:13All you're going to need is something that can edit HTML, CSS, and JavaScript files.
02:18Now I'm going to be using Aptana Studio in this course, because it's free and
02:22it's cross-platform, but it doesn't matter which one you use.
02:25Just use whatever is your favorite.
02:27You're also going to need a web server and you can use either a local one or a
02:32remote one to test your pages on, and the reason why you're going to need a web
02:36server is because jQuery Mobile makes heavy use of AJAX to load and dynamically
02:43display information to the user on their mobile device. And in order to do that
02:48you're going to need a web server.
02:49You can't just simply double-click the HTML file in the desktop or the folder
02:54that you're looking at and just run it in the Browser
02:57Now for some pages you will be able to do that just to test layout, but to see
03:01the true interactivity and to take advantage of all of jQuery Mobile's features,
03:05you will need a web server.
03:07So let's jump over to the Browser really quick and review some of the things
03:11that you're going to be using in this course.
03:13So here is the website for jQuery Mobile and this is where you will get jQuery
03:17Mobile from and we will cover that a little bit.
03:20This is the jquery.com website where you will get jQuery from.
03:23I am going to be using Aptana Studio 3 which is a free IDE that you can
03:29download from aptana.com, but again you can use whatever IDE or text editing
03:35tool that you like.
03:37Now let's take a look at some of the other courses that are related to this
03:40one here on lynda.com.
03:42If you need a refresher course on HTML and XHTML and so on, you can take a look
03:46at XHTML and HTML Essential Training.
03:49This is a course that covers the fundamentals of HTML.
03:52If you need a refresher course or if you're new to JavaScript, you're going to
03:55want to watch JavaScript Essential Training and this is a really great course
03:58for getting up and running with JavaScript, because JavaScript is an essential
04:03logic provider on the web.
04:05This is where you will build all your application logic.
04:07For mobile-related web design you can watch Mobile Web Design and
04:12Development Fundamentals, which I recorded, and if I do say so myself it's a
04:15pretty great course.
04:16And if you scroll down here you'll see that there are places where in the
04:20setting up a development environment it shows you how to set up a local web server.
04:24So if you are not familiar with setting up a local web server you can watch
04:27this course to learn how to do that and you can also watch Installing Apache,
04:31MySQL, and PHP, because this also shows you how to set up a local web server if
04:36you're not using IIS.
04:37It will show you how to set up Apache on Windows, on Macintosh and so on.
04:41You don't have to watch the whole thing, because it covers MySQL and PHP as
04:45well, but you're going to want to watch the parts on installing the web server part.
04:49That's going to be the most important part.
04:50So these are the things that you will need to be familiar with in order to
04:53complete the exercises in this course and to learn about using jQuery Mobile.
04:58So once we've got all that squared away we can get started with the rest of the
05:01course and building our applications.
Collapse this transcript
Using the exercise files
00:00Just a quick word on using the exercise files.
00:02Now if you are a premium member of the lynda.com Online Training Library or if
00:08you're watching this tutorial on a DVD- ROM, then you have access to the Exercise
00:12Files used throughout this course.
00:15The way that I've laid it out is inside the Exercise Files you will find
00:18folders that correspond to each of the chapters that are in this title.
00:23So let's take a look inside one of them.
00:24So here inside Ch_2 for example you can see that I've laid out several of the
00:29exercise files for each particular example and there are two versions of each
00:34file, there is the start version of the file, which is the version that does not
00:39have any of the exercise completed and then there is the finished version of the
00:44file, which is the complete finished version that you can run locally in your
00:48browser to see how it works.
00:51Now if you want to follow along with me as we go through the course in one of
00:55the exercises, you can start by opening up the start version of the file and
00:59then follow along with me as I add the code to the start version to arrive at
01:04the finished version.
01:05If you would rather not wait just go ahead and open up the finished version and
01:08see the code and see how it works.
01:10Let's go back to the root level of ExerciseFiles folder.
01:13You'll also notice that I've included a file here called ExampleSnippets.txt.
01:17This is all of the code snippets that I'm going to be using to complete each one
01:21of the exercises, because it's probably not very fun watching me type code.
01:26What I am going to be doing is copying and pasting the code from these
01:30examples into the start version of each of the example files in order to
01:35arrive at the finished version.
01:37Now even if you are not a premium member of the Online Training Library or you
01:41don't have a DVD-ROM, this file will still be available to you to download.
01:46So even if you don't have the Exercise Files you'll still be able to get this content.
01:50If you're a monthly member or an annual member of lynda.com then you don't have
01:55the Exercise Files, but you can follow along from scratch with your own assets
01:59and you can use the code that I'm using in the ExampleSnippets.txt to build your own examples.
02:04So having said all that, let's get started.
Collapse this transcript
1. Introduction to jQuery Mobile
What is jQuery Mobile?
00:00So what is jQuery Mobile?
00:02jQuery Mobile is a JavaScript library that provides a cross-platform way for
00:07creating mobile web applications.
00:10Now this is not in native applications, these are for mobile web apps.
00:14It's built on top of the jQuery library which is extremely popular.
00:18It's the most popular JavaScript Library out there today and it's built on top of it.
00:23So if you already know jQuery itself, then you should be very comfortable using
00:28the jQuery Mobile library.
00:30Now it relies heavily on a lot of existing web technologies like HTML5 and CSS3,
00:36JavaScript, and AJAX.
00:37So pretty much that's a lot of buzzword lingo right there, but these are the
00:42things that jQuery Mobile uses in order to accomplish its work.
00:46JQuery Mobile when it loads it automatically enhances your webpages with a
00:51consistent look across a variety of mobile web platforms, everything from iOS to
00:57Android to Windows Phone to Blackberry, it provides you mobile webpages with a
01:03consistent look and feel, a consistent way of behaving, a consistent set of user
01:09interface widgets and so on to build web applications that work the same way
01:14across a variety of mobile devices.
01:17So why would you use jQuery Mobile?
01:19Well, let's imagine that you wanted to build applications that work across
01:23today's most popular mobile platforms like iOS, Android, Blackberry, Windows
01:28Phones, Symbian, and there are others, but these are the most popular ones.
01:31Well, you've basically got two choices available to you, you can either build a
01:35native application or you can build a web application.
01:39Now if you want to build a native application then you're going to have to learn
01:41a whole bunch of technologies that are different across the different platforms.
01:46So for example, on iOS you will have to learn Objective-C and Cocoa.
01:50For Android and Blackberry you will have to learn Java.
01:52For Windows Phone you have to know C# and .NET.
01:55For Symbian you will have to learn either Qt or C++ or Java and even in the
02:01cases where the underlying technology is the same like Java for Android and
02:05Blackberry, the APIs are actually different.
02:08So the functions that you called to do things on those platforms are not
02:13going to be the same.
02:14The way that an Android device stores data for example is probably going to be a
02:18different function call than it is on Blackberry even though it uses the same
02:22underlying programming language.
02:24Now Web applications help to solve a lot of this problem, because you only have
02:28to learn HTML, CSS, and JavaScript.
02:31The main drawback to web applications is that you don't have access to a lot of
02:35the underlying native capabilities that a device might provide.
02:40So for example, a web application may or may not be able to use the device's
02:45microphone for example or the camera or access the contact list.
02:50Now those problems are being addressed by the device makers, but there's always
02:54going to be some inconsistencies among the various platforms.
02:58So by building a web application using jQuery Mobile you can in many cases
03:03achieve a lot of the same functionality and features across a variety of
03:06platforms by only learning the underlying technologies of the web rather than
03:11having to focus on the proprietary APIs for each of the devices.
03:15Okay, so let's jump over to the jQuery Mobile Gallery so you can get an idea of
03:20what you can actually do with jQuery Mobile.
03:21So here in the browser I've gone to jqmgallery.com and this is a pretty good
03:26gallery of the existing mobile sites and applications that are available that
03:30are using jQuery Mobile today, and you can see if you scroll down a little bit
03:34there is a whole bunch of categories over here on the right side, there is
03:36Business, Education so on and so forth.
03:39And if we scroll down you can see that there are some screenshots of various
03:43websites that are using jQuery Mobile today.
03:46Here is one, this is a company called Box.net, they provide a file-sharing
03:50service that lets people share files across platforms.
03:53There's another one down here.
03:54This one is one of my personal favorites.
03:55It's Chukar Cherries.
03:57These guys are a cherry farm located up in Seattle near where I live.
04:01But you can see that basically each one of these sites looks pretty good when
04:05laid out on a mobile device and it's using a common set of UI widgets.
04:09So for example, here you can see this list with these right-facing arrows, over
04:14here the same idea, list with the right-facing arrows.
04:18All of these are achieved by using the jQuery Mobile built-in UI widgets.
04:23Here is the same thing.
04:24So you're going to have a pretty consistent look and feel across a variety of
04:27websites by using jQuery Mobile. All right.
04:31So that's what jQuery Mobile is, let's take a look at jQuery Mobile's
04:34features next.
Collapse this transcript
Overview of features
00:00Let's look at the features or at least some of the features that jQuery
00:04Mobile provides when you build your web applications based on the jQuery Mobile framework.
00:10So here I am on jquerymobile.com and what I am going to do first is just scroll
00:15down and you can see that jQuery Mobile provides support for a lot of the
00:19platforms that are popular in marketplace today.
00:21Of course, iOS and Android and this BlackBerry, Windows Phone, WebOS,
00:26Symbian, MeeGo, Bada.
00:28So I'll get a little bit more into that in a moment, but take a look at the UI widgets.
00:32These are the kinds of things that you'll be able to take advantage of in your
00:35jQuery Mobile based applications just by using standard HTML5 markup.
00:40In order to get these kinds of look and feel in your applications, you won't
00:44need to do anything special.
00:45No social programming, no special CSS, it's all provided for you.
00:49Now unlike jQuery, where you have to download a separate UI library like
00:54jQueryUI, or one of the other publicly available ones, the jQuery Mobile Library
01:00provides the mobile widgets built-in for you.
01:03So it's all provided in one library.
01:04There is nothing separate to download, other than jQuery itself.
01:06So let's go back up until the top here.
01:09Now I am going to click on the Docs link, and this will take me to the
01:14documentation for jQuery Mobile, and the first thing you notice is that the
01:18layout and the UI widgets that are being used here in the documentation are
01:23pretty much some of the same ones that we saw in the jQuery Mobile gallery
01:28that were being used by the applications that are built on top of jQuery Mobile.
01:32You may probably have also noticed that I am using the Safari browser and the
01:35reason I'm using Safari browser is because the Safari browser is the most
01:39popular mobile browser that's available in the market today.
01:44It's available on the iPhone, the iPad.
01:46So I want to use the same rendering engine that my jQuery Mobile applications
01:51are going to be running into out in the marketplace.
01:55So the next thing I want to quickly show you is if I zoom out from the browser
01:58window, let's take a look at what happens when I change the size of the window.
02:03So here I've got a wide window which is pretty similar to the width of a say
02:08a tablet device in landscape mode, but when I size the window down, watch what happens.
02:13Notice how the layout goes from being optimized for a landscape orientation,
02:17down to a vertical one, like you might find on most phones.
02:21So jQuery mobile is detecting this for me, and relaying the page out to look
02:27good on a vertical screen like a phone, or on a tablet device, like say an iPad
02:34or a Samsung Galaxy or anyone of the other popular tablets out there.
02:38So just by using the jQuery Mobile Library, I am getting a layout that's
02:42optimized for the screen width.
02:44Let's go back and take a look at the supported platforms.
02:48The supported platforms in 1.0, and right now I'm using the Release Candidate
02:532 version, (RC2), but by the time you get here this will probably be a
02:55released version of library.
02:57They're currently at Feature Complete, so the features wouldn't have changed by
03:00the time you get here, but just for sake of completeness, I am using RC2.
03:05So if we scroll down, one of the things you will notice that they've graded
03:08their browser support into A grade browsers and B, and C grade and so on.
03:13The A grade browsers are the most important ones.
03:16These are the browsers that get the full mobile enhanced experience with Ajax
03:20based page loading, and animated page transitions, and so on.
03:24You can see the list is pretty long, and of course, Apple iOS and Android is
03:27on there along with Honeycomb, Windows Phone, and bunch of other modern mobile phones.
03:31The next level down is the B grade browsers, and these are the browsers that get
03:35the enhanced mobile appearance, but they don't get the Ajax navigation features,
03:40and I will get to that in a little bit, but that that's the main difference.
03:42But the important thing to notice is that under the A grade browsers, these are
03:46the most common devices that your web applications will be run on.
03:50So they will get a really great experience if they are using jQuery Mobile.
03:53Let's go back up here.
03:56I also want to quickly point out the accessibility features. Because jQuery
03:59Mobile is being built on standard HTML5 and CSS3, it's going to get a couple
04:05features accessibility wise that are based on the W3C's WAI-ARIA specification.
04:12Things like focus management and keyboard navigation and so on.
04:16Your mobile web applications will be able to take advantage of these
04:19features without having to do any special work and as jQuery Mobile improves in this area
04:24your web applications will just simply get those improvements.
04:27Let's go back to the home page and take a look at some of the basic features
04:31that jQuery Mobile provides.
04:33Let's just go to the Pages & dialog section, and I am going to click on the
04:37Single page template.
04:39You can see here in the Single page template that I've got a header, and some
04:42content, and a footer and all of this is achieved and styled, using jQuery
04:47Mobile standard markup.
04:48There is no special markup here.
04:50You can just get this appearance by using the basic semantic markup in HTML and
04:54jQuery Mobile just progressively enhances the page to look really good in a
04:59mobile context, and we will take a look at how to do that in a little bit. Let's go back.
05:03Let's take a look at something else.
05:04Let's take a look at Page transitions.
05:07In the Page transitions section, let's take a look at some of these various
05:11transitions you can get.
05:12So I am going to click on the slide, and you can see that as I click on slide,
05:17the next page kind of slides in from the left, whereas, if I click on pop, you
05:22can see it kind of pops out from the center.
05:24Well, let's see, there is Fade, One more, let's click on Flip.
05:30So these are all of the different page transitions, and again, these are just
05:35provided for you as part of jQuery Mobile. And they give a very native
05:39appearance to the way that your pages will behave in a mobile browser.
05:43Let's go back and take a look at some of other stuff.
05:45Let's take a look at some of the Form support that jQuery Mobile provides, let's
05:49click on the Form element gallery.
05:51Now these are all the Form elements that are provided in jQuery Mobile.
05:54Now just to be clear these are standard form based elements that have been
05:59progressively enhanced by jQuery mobile to look good in mobile browsers.
06:05So just by using standard form markup, you're going to get the features that you see here.
06:09So for example, there is basic Textinput, here is a Textarea.
06:14Watch what happens in the Textarea.
06:16As I type in the Textarea, see how it's automatically growing to avoid scrollbars.
06:20That is a basic feature of jQuery Mobile that your applications will just get.
06:24Here's a Search bar, and you can see that I'm getting my little Delete button.
06:30All of this is provided free of charge to you.
06:32Here is a slider control, all of this stuff is just built into jQuery Mobile.
06:40Some enhanced checkboxes, radio buttons, here is a Customize select list.
06:48This is all stuff that your Forms in jQuery Mobile will just get free of charge.
06:55Last thing I want to take a look at, let's take a look at List views.
06:59Now List views are the things that you see here.
07:01This is a List view.
07:02Each one of these items, there's a little arrow over here.
07:04This is a pretty common mobile UI paradigm that's being used on the mobile web
07:09today and jQuery Mobile devotes an entire section to supporting List views.
07:13So let's take a look some things you can do with List views.
07:16You can have Numbered List views, you can have various Dividers, here's a list
07:23that's been divided into alphabetic sections.
07:25Here is a List views that have count bubbles in them.
07:29You've probably seen these on various mobile applications used in past.
07:33They can have Icons in them.
07:35They can have Thumbnails in them.
07:38You can have Content formatting.
07:40There is a whole bunch of stuff that you can use in your List views.
07:44Again, we will have a bunch of examples that go over this stuff.
07:49So hopefully that gives you a pretty good overview of the kinds of things that
07:52you can do with jQuery Mobile in your applications.
07:55So if you're as excited as I am to start using some of the stuff, it's time to
07:59start building our first jQuery Mobile application.
Collapse this transcript
Building your first jQuery Mobile app
00:00Now to build our first jQuery Mobile application we'll get into a few things.
00:03First, we're going to need to include the three jQuery Mobile resources that all
00:08the applications need.
00:09There's one CSS file and there's two JavaScript files, the jQuery Mobile file
00:14and the jQuery file itself.
00:16We're going to mark up our content as usual.
00:18We're not going to do any special programming or any special tags.
00:21We're going to use standard HTML, standard CSS, and jQuery Mobile will just
00:25simply progressively enhance the file for us.
00:27We are going to use an HTML5 feature called the Data Attribute.
00:32HTML5 defines the ability to create these attributes called Data Attributes,
00:38which are used to attach data to tags inside a file.
00:42Now jQuery Mobile uses these to define attributes for example like data-role
00:47which identifies the role a particular piece of markup plays in a jQuery
00:52Mobile application.
00:54It's used to identify things like pages and headers and footers and content and
00:57so on, and we'll see how that works in just a moment.
01:00Now, the cool thing about jQuery Mobile is, you can include more than one page
01:05in a single HTML file.
01:08So only one file needs to be downloaded from the server but it can have more
01:11than one "jQuery page" in it that can be transitioned to and from and it gives a
01:18really smooth appearance to your application and it makes it look like it has
01:21more than one page in it, when in reality, it's just one HTML file, so we'll see
01:26how that works in just a moment.
01:27All right, let's jump over to the code.
01:29Okay, so I am here in my Aptana Studio Editor and I have got my ExampleSnippets
01:33file open and I've got my first_jqmobile _start file open and you'll notice also
01:40that in my Snippets I have got some paths to my jQuery Mobile related resources.
01:46Now one of the things I want to point out is that if you've downloaded the
01:49exercise files to use and follow along with, everything should be okay
01:53because we've included jQuery and jQuery Mobile in the distribution and all
01:57the paths should line up.
01:59But, if you downloaded a different version of jQuery or jQuery Mobile, then
02:03you may need to edit these paths right here to point to the right version of the library.
02:09But, that's only if you've downloaded the jQuery Mobile and jQuery library
02:14yourself, if you're using the exercise files, everything should just be fine.
02:18Let's jump over to the start file and you can see that this is just an empty
02:22HTML file right now, there's nothing special about it, it's just regular HTML
02:25code and what we're going to do is copy some of the Snippets over.
02:28So the first thing I am going to do is copy the things that jQuery Mobile needs,
02:34copy these and paste them in the head.
02:39So this is the style sheet right here that points to the jQuery Mobile styles
02:43that enhance all the page elements. Here's the jQuery library itself, and that
02:47has to come before the jQuery Mobile library because the jQuery Mobile library
02:51depends on things that are defined in the jQuery library, so it has to come
02:55after jQuery has been included. All right.
02:58So once we've done that, let's go back to the Snippets.
03:01Now, I am going to start here on line 13, I am going to copy these lines here
03:07and paste them in to the body section. And these are standard HTML5 constructs;
03:14the section, the header, the footer down here, this is not special markup, this
03:18is all HTML5, and you can see here that I am using these data-role attributes
03:23to identify different parts of this construct that jQuery Mobile is going to
03:28need to know about.
03:29So on the top here, this section I have defined a data-role attribute that has
03:35the value of page, and what that means is this entire section right here
03:39constitutes one complete jQuery Mobile page.
03:44So this will be shown as one page inside the Browser.
03:47Now, inside that page, I've got a header, and I've got a data-role on there that
03:52identifies the fact that it is a header, and jQuery Mobile will use that to mark
03:56up and style the header section.
03:58Here, I've got a div which is wrapping the content and there's a data-role
04:02attribute that says this is content, and then I've got a footer down here which
04:05is identified as a footer and jQuery Mobile will style that as well.
04:10Okay, so let's go ahead and save this and let's just run it in Safari.
04:13I've got Aptana Studio set up to do that, so I am just going to run it in my
04:17Safari Browser, you can run it in your Browser locally as well.
04:21And when I run it, you'll see that when it comes up in the Browser, I've got a
04:25header, a footer and a content section, and all of this is being styled
04:30automatically by jQuery Mobile.
04:31I didn't have to do anything special, it all just gets progressively enhanced.
04:35So let's go back to the code. So that's one complete page.
04:40So let's go back to the Snippets.
04:41I am going to scroll down, and this time, I am going to copy the second page
04:49right here, go back to the code, paste this in.
04:55So now I have got two jQuery Mobile pages.
04:59This one is defined by a section, this one is defined by a div.
05:02It's not really important, you can choose to use a div, you can choose to use a
05:05section, it doesn't really matter as long as it's a container element that can
05:10be identified as a page using the data-role attribute.
05:13Now inside, I have another set of content that looks pretty similar to the
05:18first one, I've got a header, I've got a content area, I've got a footer, all the same stuff.
05:24Notice also I've got links in here.
05:26Now, here I have got a link that says, Go to the second page, here I have a link
05:30that says, Go to the first page.
05:32The other thing you notice is that inside the href for this link, I've got a
05:36named anchor that's referring to the ID of this second page right here.
05:42Now, I don't have to do anything special, jQuery Mobile is going to look through
05:46all the links on the page and identify the ones that have hrefs that refer to
05:52internal structures like this div that have an ID that match their href.
05:57So when I click on this link, jQuery Mobile is going to intercept that click and
06:01say, oh, this page is actually defined inside this HTML file, load this content
06:07here and then animate it into place as if it were a separate external page.
06:12And then similarly down here in second page, I have an href that goes back to
06:16the first page and this says, go back to the first page, so the same thing.
06:18So these two links I am going to refer to these two different pages.
06:22I am going to save, I am going to run this in the Browser.
06:25So now when I click on the Go to the second page, watch what happens.
06:30See how the second page has got animated into place, even though it's in the
06:34same HTML file, it's being treated as a separate page.
06:37If I click Go to first page, you'll see we go back to the first page.
06:41This is one of the really cool features about jQuery Mobile.
06:43I have got more than one content page defined in one HTML file.
06:48So this should give you an idea of some of the power that jQuery Mobile is
06:52going to provide for your mobile web applications and we'll get much deeper
06:56into this as we go throughout the course but this should give you a pretty good
06:59taste of what's to come.
Collapse this transcript
2. Building jQuery Mobile Pages
Understanding page architecture
00:00Let's take a more detailed look at the jQuery Mobile Page Architecture.
00:04So as we saw previously when we built our first jQuery Mobile application,
00:09there is a way to define more than one application page in an HTML file. And jQuery
00:15Mobile treats pages in two basic categories, there's what's called internal
00:19pages and external pages.
00:22So let's quickly review the internal pages like we saw in the previous example.
00:27Now, if you take a look at the HTML code and you scroll down and you look at the
00:31different section and div that I had in there, there were these attributes named
00:35data-roles and I've highlighted them here so you can see them, and each one of
00:39that data-role attributes identified an individual page that jQuery Mobile would
00:46parse and then display inside the browser viewport at a given time.
00:51So even though it's one HTML file, there's two pages in this file, and inside
00:57the first page you can see here I've got a regular link that uses a named
01:01anchor to refer to the second page.
01:04So if you take a look at this in block form, you have the HTML file which is
01:08this big rectangle right here and then inside you've got these two different
01:11pages, you've got Page 1 and you've got Page 2, and in this case, the named
01:15anchor is referring to the second page, and for this reason you can't use
01:20regular named anchors inside your HTML pages, inside jQuery Mobile because it
01:26uses this mechanism to refer to individual pages inside the given HTML file.
01:33So if you're using named anchors to refer to destinations inside of page, then
01:39you'll need to find some other mechanism to make that work because jQuery Mobile
01:42will interfere with that.
01:44Now, along with internal pages, there is a concept known as External Pages.
01:49Now external pages are pages that are linked to normally, so in this case you
01:52have a link that refers to an external page, and this could be a link to any
01:57other page anywhere on the Internet, in this case I've got a relative URL here
02:02that refers to a page in the same directory as the page this is linking from.
02:07Now, jQuery Mobile will attempt to treat this the same way as it would an internal page.
02:14The only difference is that jQuery Mobile will attempt to load this page's
02:17content via an AJAX request for a smooth transition.
02:22So the content will be loaded and then parsed and then added to the DOM of the
02:27current HTML file that's in the Browser window currently.
02:31When that happens, all the widgets that are in the page are auto-initialized by
02:36the jQuery Mobile framework.
02:38So anything like headers and footers and content sections and forms or list
02:44views or whatever else you've got in the page is going to be initialized by the
02:47framework, and then it's animated into place via a transition.
02:52So the appearance to the end user will be the same as if the page had
02:57been defined internally.
02:59Now, there's going to be times when you don't want this behavior, maybe you're
03:02linking to a page that's outside of your particular app or site or for some
03:06reason you just don't want the page to be loaded using this AJAX mechanism, and
03:11you can turn it off.
03:13So if you want to link without the AJAX behavior, all you need to do is specify
03:17one of these three things on your link.
03:20You can say rel="external", you can use the data attribute called
03:24data-ajax="false" or you can give it a target like blank or some other target name.
03:31And jQuery Mobile will realize, oh, I don't need to load this via AJAX, I am
03:36going to load this as a regular external page and just let the Browser do its thing.
03:42So let's jump over to the code and see this in action.
03:45So here in Aptana Studio I've got my Snippets open and I am scrolled down to
03:49Chapter 2 and I have also got my pagenav_start file open and you can see I've
03:55already gone ahead and included the libraries for you and I've defined a section
03:59here that identifies a page.
04:01So let's go back to the Snippets, and let's copy and paste these lines right here.
04:05So I am going to copy these, I am going to paste them inside the section below the header.
04:12So inside my content section I now have a couple of links that refer to external pages.
04:17Now, on the first one here, I am referring to the external page without any
04:21particular declaration, there's no attribute saying what to do about AJAX.
04:25So this is a link to an external page and here I have a link to another external page.
04:29Only in this case I have got rel=" external" and we'll see how these two
04:33behave differently.
04:35So I am going to save this and I am going to run this in the Browser, and you
04:39can see that when Safari loads, so here's my page content.
04:42Now, when I click on link to an external page, you can see that the external
04:47page is loaded, and then animated into place, just like you would expect for an
04:52internal page, and in fact, let's go ahead and look at what that external page
04:56is made up of, that's this guy right here.
04:58So externalpage1 is just some content.
05:01Now, you notice also that when I animated the page into place, the only
05:05content that became visible was, what's inside this data-role="page" section
05:11inside the external file.
05:13So we'll come back to that in a second but I just wanted to point that out
05:15just for the moment.
05:16All right, so let's go back to the Browser.
05:18So now I am going to click on Link to another external page.
05:23In this case though, I am using the rel="external" link right here and you
05:28notice how that page was just simply directly linked to, it was not loaded
05:32via AJAX, it was not animated into place for the transition, whereas this one
05:36is, right, this one is not.
05:40So just to show you the code for the second external page, that's this guy right
05:45here and you can see that this content is being loaded from outside the
05:49framework, it's just being loaded as a regular page.
05:52Now, let's go back to the first external page.
05:55What's going to happen is when jQuery Mobile loads this content, it's going to
05:58look for a container tag like a div or a section or something that has a
06:03data-role="page" attribute.
06:05Now, if that's not there, just take this off, now I am going to save that and I
06:12am going to go back to my example and run it again.
06:14I am just going to refresh to make sure we're getting to the new page.
06:17Now, when I click link to an external page, notice how all the content got loaded.
06:22So if that data-role="page" is not present, then what happens is jQuery Mobile
06:27just takes all the content that's in the body section and creates a new page
06:32out of it, inserts it into the DOM of the current file and then animates it into place.
06:36so if you don't want that behavior, then what you need to do is surround the
06:42content that you want to constitute the new page inside a container tag with a
06:48data-role attribute that's equal to page.
06:50So in a nutshell, that's basically how jQuery Mobile specifies its page
06:56architecture into internal and external pages and how it loads the content.
07:00And you have full control over how you want the content to be loaded.
07:03If you don't do anything special, jQuery Mobile will just simply handle all the
07:07links of the page for you, load the content via AJAX and animate it into place
07:11regardless of whether it's an internal or external page.
07:14However, in cases where you don't want that behavior, it's enough to turn
07:17it off.
Collapse this transcript
Using page transitions
00:00jQuery Mobile gives you control over how you want to transition from one page to
00:05the next and you probably recall from earlier I mentioned that there are a bunch
00:10of different transitions you can use and you can specify which ones you want to
00:14use on a per link basis when transitioning from one page to another.
00:19Now jQuery Mobile provides some built-in ones, they rely on CSS3.
00:24They're not available on browsers that do not provide support for CSS3
00:29transitions and at the time of this recording that's IE 9 and I believe
00:35Firefox does not currently provide CSS3 transitions, but the good news is that
00:39most mobile WebKit based browsers do, and so on most devices the transitions
00:44will work just fine.
00:46Now what I mean by that is the page will still be transition to, the link will
00:49still happen, but the smooth animation will not occur on browsers that don't
00:55provide CSS3 based animations.
00:58So there are six such transitions that you can use and I've listed those in a table here.
01:03There is fade and flip, pop, slide, slideDown, slideUp.
01:08You have control over this on a per link basis as I mentioned.
01:12Now each one of these pretty much does exactly what its name implies and
01:16perhaps the best way to see how each one behaves is to actually go into the
01:21code and try it out.
01:22So let's just jump over to the Editor.
01:24All right, so I'm in here in my pagetransitions_start file and I've got my
01:29Snippets file open right here and I scroll down to the Page Transitions section.
01:34Now in the start version of this file I have a pre-built second page and this is
01:39the page that we're going to be transitioning to and inside that page I've got a
01:44header and a content section and inside the content section I have a link right
01:49here which is going to take us back to the first page.
01:51So let's go to the Snippets and what I'm going to do is copy in the code that
01:57represents the first page.
01:58This is the page that we're going to be transitioning from, so I'll copy that
02:04and we'll paste it in here right where the first page goes and Save.
02:08Now the way transition works is you specify the transition using a data
02:12attribute named data-transition and the value for that particular
02:17data-transition attribute is the name of the transition you want to use.
02:21So for example, you can use fade, flip, pop, slide and so on.
02:25So let's try some of these out, so I have copied the code in and save it and now
02:28I'm going to run it and I'm going to run it in Safari, so make sure we're using
02:32a browser that supports transitions.
02:35So here we are in the browser and you can see that we got a whole bunch of links
02:38to try out some different transitions.
02:40So let's see what Fade looks like and you can see that when we click the Fade
02:44link, you get a fade to the second page, and if we could do a Flip, right, there
02:48is the Flip animation.
02:49Let's see what Pop looks like, see the second page pops into place from the
02:54center of the screen.
02:55There is a slide, that's the default, and by the way you can change the default
02:59animation and we'll take a look at that later on when we get to the section on
03:03customizing jQuery Mobile's configuration.
03:05In addition to sliding in from the side we can slide down and we can slide up.
03:12So these are all the transitions that are available to you built into jQuery
03:16Mobile and if you are feeling adventurous you can build your own.
03:19There is a plug-in library just like you can use in regular jQuery, you can go
03:24build your own transitions.
03:25The jQuery Mobile source code is open for you to look at, so you can see how the
03:28current transitions are built, but in a nutshell that's how jQuery handles
03:32transitions from one page to another, it's as simple as specifying a
03:36data-transition attribute on the link that causes the second page to be loaded.
Collapse this transcript
Creating and using dialogs
00:00We're going to wrap up our section on examining jQuery Mobile's page
00:03architecture by looking at pages as dialogs.
00:07In jQuery Mobile you can show any valid page as a mobile dialog, and the way
00:13that you do this is simply adding a data attribute named data-rel with the
00:17value of dialog to the link that opens the page that you want to be opened as a dialog.
00:23So in the code it would look like this.
00:25You would have a link and in this case we have an href to a internal page named
00:29secondpage and that code in the red there, that data-rel=dialog, that's going to
00:33open the link when it's clicked on or tapped on by the user as a dialog on top
00:39of the opening page.
00:40So let's see this in action.
00:42We are going to jump over to the code.
00:44Here in my Editor I've got the using_ dialogs_start version opened and I've also
00:50got my Snippets opened and I've scroll down to the Dialogs section here.
00:54So let's take a look at how this works without the data-rel=dialog attribute.
01:00So I'm going to Save this and I'm just going to run it as it is and you can see
01:04that when the page loads we have a page 1 here.
01:07I'm going to click on Display Dialog and without the data-rel attribute the
01:12second page just animates in as if it were a normal internal jQuery page. So
01:18let's go back to the first page.
01:19All right, now let's copy the code here.
01:23So what we're going to do is copy this line and back in our start version, we're
01:26going to replace this link right here with this new one and you can see that the
01:31only difference is that here I have a data-rel=dialog on my link.
01:37So let's get rid of this one and save.
01:39Now let's run this again.
01:41Now when I click on the Display Dialog link, you can see that what happens is a
01:45dialog comes up with the content displayed in the page and you can see with this
01:51little pop animation that comes up from the center of the page as a close box
01:55and it behaves as if it were a dialog.
01:57And you can do this with any page that is a valid jQuery Mobile page, you simply
02:02add that attribute data-rel=dialog and you can display the page as a Dialog.
Collapse this transcript
3. Working with Buttons and Toolbars
Using headers and footers
00:00So now that we've seen how to build jQuery Mobile pages, and we've taken a look
00:03at the jQuery Mobile page architecture, we're going to spend some time now
00:07looking at the content of those pages and we're going to start off by looking at
00:12toolbars specifically.
00:13Now, in jQuery Mobile, there are two types of toolbars, and in fact,
00:16you've already seen them;
00:17they're headers and they're footers.
00:19So if you take a regular jQuery Mobile page, the header usually goes at the top,
00:23and the footer usually goes at the bottom, and they typically serve as the first
00:27and last elements in a jQuery Mobile page definition.
00:31I mean page definition as in data-role ="page", I don't mean HTML file, I mean
00:36a jQuery Mobile page.
00:38Now, toolbars can be positioned in a few different ways and we'll take a look at
00:41that a little bit later.
00:42They can also contain buttons and navigation elements in addition to the text
00:48which is what we've seen up until now.
00:50So let's take a look at how to add buttons to our headers and our footers.
00:54So in a header, I can add buttons on both the left-hand and the right-hand side
00:58and the way that you do that is if you take a look at the way a header is
01:03typically defined, and here I have a div with the data-role="header", all I need
01:08to do is add a couple of links.
01:10In this case, I've got two links;
01:11I've got a link on the top and a link on the bottom, and they both have a
01:15data-role attribute which is equal to button and you can see Btn 1 is on the
01:20top, and Btn 2 is on the bottom, and the way this works is jQuery Mobile will
01:24parse the header and the order it comes across the buttons is the order in which
01:29it will assign them in the header. So Btn 1 is first.
01:32So it will go on the left-hand side and Btn 2 will go on the right-hand side.
01:37Now, that's all fine and good, but there are going to be times where you only
01:41want one button, and you want it on the right-hand side.
01:44So how do you do that?
01:44Well, the way you do it is you've got your header, and you want a button
01:48like this on the right.
01:49All you need to do is define one link like this, and add a class of
01:55ui-btn-right and so in this case, jQuery Mobile will parse this and realize
02:01there's only one button.
02:03But, instead of putting it on the left, it'll put it on the right, because it
02:06has that class definition.
02:08Now, like headers, footers can also have button content, but footers are less
02:13structured and designed to be a bit more flexible than headers are, and the
02:16reason for that is because headers are typically used for text and titles and
02:21maybe one or two buttons and footers in many web and native app paradigms are
02:28where you put the functional buttons for the application.
02:31So the way that, that works is, if I have a page here with a footer, normally I
02:35would define the footer like this, this is something you've already seen.
02:38Now, to add buttons to the footer, what we can do is just define some links.
02:43Now, in this case, I've got three links;
02:45Btn 1, Btn 2, and Btn 3 and they've all got a data-role with a value of button
02:51and that will cause three buttons to appear in the footer.
02:55Now, I mentioned earlier that footer toolbars are more flexible, there is no
02:59concept here of source order, or using a class to define, left, or right or
03:05anything like that, you can just define the buttons and they'll show up in the
03:09footer as you define them.
03:11So let's jump over to the code and actually see this working.
03:14So here I am in my editor and I've got my snippets file open, scroll down to
03:18Chapter 3, and here's my headers and my footers, and I've also got my
03:21headersfooters_start file.
03:24You can follow along with me if you've got the Exercise Files.
03:27So what I'm going to do is go to the Snippets, and the first thing I am going
03:30to do is copy this header right here, I am going to paste it into the top of my page here.
03:36Okay, so now I've got my header defined, and I've got two buttons, Home and
03:41Options, and you can see that I've just assigned a couple of hrefs here that
03:45don't really do anything.
03:46Ordinarily, I would use script code to assign some function to be executed
03:51when they're clicked or something like that, but this is just for
03:53demonstration purposes.
03:55So I'm going to save this and run it in Safari, and you can see that when we do
03:59that, we've got two buttons, we've got Home, and we've got Options, and we've
04:02got two buttons because here in source order, we've got the Home and the Options links.
04:06So let's go back to the code, and let's modify this is a little bit.
04:10We're going to take the definition for the Options link, I am going to copy that
04:15and I'm going to get rid of the first button for Home.
04:18I am going to replace the link for options with this one and you can see here
04:21I've got a class of ui-btn-right.
04:24So since there's only one button now, it will go on the right-hand side instead
04:28of the left-hand side.
04:29I'll run this and you can see the Home button goes away and now I have my
04:33Options button which is exactly what you expect, and if I took out this class,
04:38and save it, and then run this, you'll see it moves over to the left which is
04:42what you would expect.
04:44But, I'm going to undo that, put it back in, and you can see that, that cause it
04:48to jump to the right.
04:50Okay, let's move on to the footer.
04:53So here in the footer, I am going to copy that, and paste it down here below the content.
04:59Now, in this case, I've got three buttons, and I'm going to save this, I am
05:03going to run it again.
05:04Here in Safari, you can see I've got Button 1, 2, and 3.
05:07Now, the other thing you notice is that up here in the header, I've got some
05:10nice padding around the button, there is some spacing here.
05:14Down here in the footer, I don't have that.
05:16The buttons are pretty much flush with the edge of the footer content and I want to fix that.
05:21Now, the way that you do that in jQuery Mobile is there's a special class you
05:25can use and back over here in the Snippets, I'm going to copy this
05:28class="ui-bar", and I am going to put that on my footer definition right here.
05:34This tells jQuery Mobile to add some additional padding and spacing into the
05:39footer bar that it's applied to, to give the buttons, and content a little bit more room.
05:43So when I save and I run, you can see that now we've got some more padding and
05:48spacing, and it just looks a lot nicer.
05:50So that's just a brief introduction to using header and footer toolbars and
05:54adding content like buttons in addition to regular text.
Collapse this transcript
Creating navigation bars
00:00Now let's look at creating navigation bars.
00:02To create a navigation bar, you use the data-role attribute with a value of
00:08navbar and that will define a navigation bar.
00:11And any links that are inside the element, that has this data-role applied to
00:16it, will be styled as a navigation bar.
00:19So let's take a look at what that looks like.
00:20So here we have a definition for a header, and that will create a header just as
00:25we've always seen up until now.
00:27Now if I use a navigation element and you can use navigation elements because
00:31we're working with HTML5 here, but you don't have to use a nav, you can use a
00:34div if you want to, but in this case I am using a nav element and I am giving it
00:38a data-role of navbar and that will define the navigation bar area below the
00:44header, and then inside I am going to put three links.
00:47And in this case, I've got three buttons, One, Two and Three and that will
00:51define navigation bar that looks like this.
00:55Now, if you want, you can make the navigation buttons look a little bit
00:58different because by default, the navigation buttons are as wide as
01:03whatever their contents are.
01:05But if you want to create equal-width buttons then all you need to do is wrap
01:09them in an unordered list.
01:11So we will take the same definition we had before which is the header and then
01:14we have the nav which creates the nav area.
01:16And now I am going to create an unordered list with list items, with each one of
01:20the list items content being a link that is a button.
01:23In that case, I will get buttons that look like this, that span the entire width
01:28of the page, all of them set to be the same equal-width.
01:32So let's take a look at this in action over in the code.
01:35So here in the code I have got my Snippets and I'm down in the navigation bar
01:39section and I am going to open my navbar_start.
01:43And so you can see here I have got two pages, this is the first page, this is the second page.
01:47And I've got a header here and I have got some content.
01:51So what I am going to do is go over my Snippets and I am going to copy this
01:54section right here which is a nav that has a navbar on it.
01:57And I am going to copy that and want to paste it in right where it says
02:02navbar goes here, so in this case, we are putting the navbar inside the header definition.
02:08You can also put the navbar inside the content if you want to as well, but in
02:12this case, we want it to be part of the header controls.
02:15You might also notice that I've got a couple of other things that you
02:17haven't seen before.
02:19So for example, I've got a data-icon attribute and the icon attribute applied to
02:26a button, specifies an icon that you can associate with the button content.
02:31And we will get to this later on when we talk about defining buttons.
02:34But I just want to show this to you now to just give you an idea of what's
02:37possible when you create navigation bars.
02:39Okay, let's go back over to the Snippets and I am going to copy the other
02:43navigation bar, and I am going to copy that and I am going to paste it into my
02:50second page here, so I am going to save and now we are going to run.
02:56But before I run, notice how in the navigation bar here, I've got a link to the
03:01second page, on the two buttons, so this button will lead to the second page and
03:06down here my link leads back to the first page.
03:09So I've got the toolbar navigating to and from a couple of different pages.
03:14So let's save, and let's run this.
03:17So you see here is my Header Bar up here and here's my Navigation Bar.
03:21And you can see that all of the buttons are of equal-width.
03:24And the reason they are of equal-width is because back here in the code, you
03:27can see that I've wrapped them in an unordered list, with list items containing
03:32each one of the links.
03:33If I didn't have this unordered list, then the buttons would be all whatever the size they are.
03:38So let's go back to the Browser.
03:40Okay, so back here in the Browser, you'll notice that if I click on a button it becomes active.
03:45So now it's highlighting.
03:46If I click on the number Two button, it transitions to the next page.
03:50So if I click back on One, you can see I'm transitioning between pages One and Two.
03:54Now the Three and Four buttons don't do anything.
03:56I've only got links hooked up to the first two, so I will leave those guys alone.
03:59But you notice that there is a couple of things missing here, first the button
04:03gets highlighted, it doesn't stay highlighted on the second page.
04:06We need to fix that.
04:08So let's go back to the code and go back into the Snippets.
04:12Now what I am going to do is take this link right here and this is my link
04:19for my number one button on my first page so I will copy that, and I'll paste
04:24that into my first page.
04:26I'll save and will do the same thing on the second page, that's this Snippet
04:31right here, and that on my second page, goes in here and that's for my number
04:41two button, so I will take out the two button here.
04:45So what have I done here?
04:46Well, I've replaced the links for those two buttons, with a link that has a
04:53couple of classes on it.
04:54The first one is ui-btn-active, and the second one is ui-state-persist.
05:00Now the first class says, hey when you're on this page, have this button start
05:05out as the one that's being shown as the active buttons.
05:08So it's highlighted with that active button color.
05:10This class here means persist the state of that button between pages, don't get
05:16rid of the fact that that button was clicked on.
05:19So let's save and let's see what the effect is.
05:23So you notice that when page one loads for the first time, you'll notice that
05:26this button here the One button is shown as the active button.
05:30And that's because, it has the ui-btn-active state on it.
05:34So it shows up to begin with as the active button.
05:37And when I click on the number Two button watch what happens.
05:40So first, you will notice that the Two button is now active on the second page,
05:44again because it has that btn-active class on it.
05:48But now, watch what happens when I go back to the first page.
05:51When I go back to the first page, the state of that button is now persisted.
05:54It's not shown in the off value.
05:57If I click on Two and click on One, you can see that the active button state is
06:01now being persisted between those two pages and it also works, if I go back
06:05using the Back button.
06:07So if I go back and forward, you'll notice that those states are now being
06:10maintained, between those two pages.
06:13So using jQuery Mobile, you can define some pretty good looking navigation bars
06:18using standard markup and just a couple of CSS classes that the library provides for you.
06:23Along with some additional information that can do things like add icons to your buttons.
06:28You can see that by using jQuery Mobile, we are starting to approach some native
06:31look and feel for a web application.
Collapse this transcript
Understanding how positioned toolbars work
00:00I mentioned earlier that toolbars in jQuery Mobile could be positioned a
00:04few different ways.
00:05And there are three kinds of positioning toolbars that jQuery Mobile supports,
00:08there is Inline, there is Fixed, and there is Fullscreen.
00:12So let's take a look at each one of these.
00:14The Inline case is the default setting.
00:17In this case the toolbars namely the headers and footers will scroll inline with
00:22the page content, so if the content of the page is long and requires scrolling
00:27then the header and the footer whatever you defined will scroll off and onto the
00:31page along with the rest of the content and that's what you get by default.
00:35Now the Fixed option allows you to specify that the toolbars are always at the
00:40top and the bottom of the page, and the way this works is the toolbars will
00:44disappear momentarily when scrolling starts, but when scrolling stops they will
00:49reappear at the top and the bottom of the page, and if you tap the screen the
00:54toolbars will hide and show on each tap.
00:58Now the Fullscreen option is a little bit different, it's a little more subtle.
01:01It works the same way as the Fixed positioning does but the difference is that
01:05the content of the page takes up the entire screen and the toolbars sit over the
01:10the content and like in the Fixed case tapping the screen will show and hide the
01:15toolbars and when you scroll they'll go away and come back.
01:18But the only difference here, and it's a little bit subtle is that the content
01:22of the page will not start out below the header and appear above the footer.
01:27It's intended for those kind of scenarios where you have something like a photo
01:30viewer, or some other scenario where you want the entire content of the page to
01:35be visible in the viewport and allow the user to show and hide the toolbar so
01:40that the content takes up the entire screen.
01:43So let's actually see this in action, I am going to jump over to the code, so
01:46here we're in my editor and I've got my navposition_start file opened and in the
01:50Snippets I'm here in toolbar Positioning under Chapter 3.
01:54So let's just run the code the way it currently is.
01:57You'll see I have my header here and my footer down here with nothing special,
02:02so let's just run this.
02:06And you'll see that as I scroll the header and the footer are just kind of
02:10scroll into and out of view.
02:12There is the header there,
02:13there is the footer there, pretty much what you'd expect.
02:18Okay, let's try some Fixed positioning, so I'm going to go into my Snippets, I'm
02:22going to copy that line and replace my header definition here, and I'm just
02:26going to replace it with this one.
02:28You can see that basically all I did was add this data attribute called
02:31data-position="fixed" and I'm going to do the same thing on my footer.
02:35So I'll scroll down here and replace that.
02:41So now I've got fixed position headers and footers, so I'm going to save and
02:45we'll scroll back up.
02:47Right now let's run this and see how the behavior is a little bit different.
02:50So you can see that the Header Bar starts out in the same position and here's
02:55the content, and the Footer Bar appeared at the bottom of the page when the page
03:00loaded, and as I start to scroll you'll see that the header and footer go away,
03:05and when I stop scrolling they come back.
03:08So as I scroll they disappear and reappear.
03:11And if I tap-in the page they momentarily disappear and then when I tap again
03:16they come back again.
03:17So that's how you create Fixed position headers and footers.
03:21Let's go back and take a look at the Fullscreen option.
03:25Now in this case the Fullscreen option says I want the content of my page to
03:30take up the entire screen.
03:32So what I'm going to do is go and copy this Snippet here and I'm going to
03:36replace my page definition with this one.
03:39And you can see that it's pretty much the same, the only difference is that I
03:42added an attribute here called data- fullscreen and gave it a value of true.
03:47So what that basically means is, all of the content now is going take up the entire screen.
03:52So if we run this again, now it may be a little bit subtle to see in your
03:56screen but the page content is now up under the Header Bar, it doesn't start
04:01below the Footer Bar anymore.
04:03And you'll notice that when I scroll I'm getting the same behavior as I was before.
04:08You see that the header and footer go away and come back when scrolling starts and stops.
04:13The only difference is that the content is now starting out taking up the entire
04:17page and you notice that when I tap you see the headers and footers go away, and
04:21when I tap again they come back.
04:22So the only difference is that the content takes up the entire rendering area
04:27of the page but other than that it works pretty much the same way as Fixed position toolbars.
04:32So that's how you can create headers and footers with varying kinds of positions
04:37using jQuery Mobile.
Collapse this transcript
Button basics
00:00Buttons are probably one of the most common UI elements that you'll find
00:03in mobile applications either web or native, and that's because most of
00:08the interaction that the user has with the application is by tapping with their finger.
00:13So jQuery Mobile provides a whole bunch of features for working with buttons,
00:19and it makes working with buttons really very easy.
00:23You can style links as buttons, you can use form button controls, either the
00:28button tag or the input types.
00:30jQuery Mobile gives you control over how buttons are displayed, you can group
00:34them as either vertical, or inline groups, you can group them together into sets of buttons.
00:41There's a whole bunch of options for working with buttons.
00:44So let's take an example, here is a button in jQuery Mobile and to get a button
00:49that looks like this, there is a couple of ways I can do this.
00:53I can use the Button tag, I can use input type ="button", I can use a link with
00:58a data-role of a value of button in it, and either one of those three options
01:03will give me the result that you see here where I have a clickable button.
01:08Now, by default, buttons take up 100% of the available screen width unless you
01:15specify a data attribute called data- inline, and give it a value of true.
01:20So you have a page that looks kind of like this, then you're going to get a
01:24button that's the full width if you just define it like this.
01:27So if you want to have a button that's only as wide as its content, you need to
01:31have that attribute there that says data- inline="true", and that will give you a
01:35button that's only as wide as its content.
01:38You can also define button control groups and you can group them together in
01:43either horizontal or vertical lists.
01:46So for example, if you have a page and you want to have three buttons together,
01:49then we'll click this in a group.
01:51You simply wrap them inside a container element like a div and give it a
01:55data-role attribute of control group and that will group all three of the
02:00buttons together in a vertical list.
02:02Now you can also group them together in a horizontal list like this, and to do
02:06that, you simply say data-type=" horizontal" on the same container element that
02:11has the control group for its data-role.
02:14And jQuery is smart enough in each of these cases to take off the rounded
02:18edges in each of the buttons and make them look really nice and flush against each other.
02:24In addition to grouping controls in this case buttons together, you can specify
02:29things like icons for buttons, and the way you do that is you use a data
02:33attribute called data-icon and you give it the name of an icon that you want to
02:38assign to the button and you can use any one of the names that's available here
02:42in this table, these are the buttons that ship with jQuery Mobile, the button
02:47icons, and you can also specify where the icon should go.
02:50You can specify that the icon should go on the bottom, the left, right, or top
02:54of the button, and if you use the notext option, then the button will have no
02:59text in it, it will be just the icon by itself.
03:03So lots of different options for working with buttons and you're not limited by
03:07the way to the icons that just ship with jQuery Mobile, there are ways of using
03:12third-party icon libraries.
03:14That's a little bit beyond the scope of this particular course, but I just want
03:17you to know that it is possible to use third-party icon sets.
03:20It involves just naming things correctly and creating CSS files that use the
03:25particular icons and making sure that the Sprites are the right size like
03:2918x18 icons and so on.
03:31Okay, so now that we've seen how to specify a whole bunch of button options,
03:35it's time to actually work with them in the code.
Collapse this transcript
Working with buttons
00:00Let's take a look at what we've learned about buttons so far, and actually
00:03put it into practice.
00:04You can see here in the code editor, I've got my buttons_start file open and
00:08I've got my Snippets, and I'll scroll down here to my Buttons section.
00:11So let's just start out by creating some buttons.
00:15The first thing I'm going to do is copy this one right here and in these three
00:19lines, I'm going to paste those into my content section down here, and what
00:26this is going to do is create a button out of a link, and you've seen this
00:31before elsewhere in the title, but for now, we're just going to do a little bit of a review.
00:35Here, I've got a link with an href of nothing, so nothing is happening here, and
00:39a data-role attribute of button.
00:40So when I run this, you can see that I get a Link Button that's been styled from a link.
00:46So far, so good; let's move on.
00:48So let's copy the next version, and in this case, we're going to be creating a
00:54button out of a button element.
00:57So you can see here that instead of using a link, I'm now using the button tag,
01:01and we'll run this again, and now we have our button element.
01:05So the results are the same.
01:06We've got two buttons now just created in different ways. All right.
01:10Let's go back to the code.
01:13Let's take a look now at vertical versus inline buttons.
01:17So I'm going to copy these guys right here and we'll paste them down here below the others.
01:24So I've got four buttons.
01:27Here's Button 1 and Button 2 and they are both being created from links with
01:31data-role attributes of button.
01:33The only difference is that in the second group, I've got data-inline="true".
01:37So let's see what effect that has when we run this.
01:41You'll see that the first two buttons take up 100% of the width of the available
01:45screen, whereas these two buttons appear in line with each other.
01:49So that's the effect of using the data- inline attribute to specify that buttons
01:54should go next to each other rather than vertically stacked.
01:58Okay, let's continue.
02:01Now, we're going to group the buttons together in a control group.
02:06So I'm going to copy this, and I'm going to paste down here.
02:13So here, I have a container, in this case, a div, and it's wrapping these three
02:19buttons, and I've got my data-role attribute that specifies this is a control
02:24group, and I've got a data-type attribute that specifies horizontal.
02:30So what I'm going to do here is run this.
02:33You can see that there's a, b and c buttons and notice how button a and button
02:38c have the rounded corners but the middle button, button b does not have the rounded corners.
02:43The corners have been removed because JQuery Mobile is smart enough to
02:47realize that, that's the middle button there and it doesn't need to have the
02:51rounded corners applied.
02:53If I go back and either change this to vertical or just take it off because
02:59vertical is the default option, I run this again, you'll see now in this case,
03:04the buttons are now vertical, but once again, the rounded corners are only
03:08being applied to the first, and last button in the group, and the middle button
03:12has a nice square effect.
03:14So that's what wrapping these elements in a control group does for you.
03:18Okay, let's go back, and let's change that back to horizontal. All right.
03:24Let's move to the next example.
03:25Let's do some work with some icons.
03:27I'm going to copy these examples right here, and I'll paste these down here in the bottom.
03:35So now, we've got some buttons, and in this case, I'm using the data-icon
03:43attributes to specify what icon should go with each button and I'm using the
03:48data-iconpos attribute to specify where the icon should appear on the button.
03:53There's top, bottom, left, right, and notext.
03:56So let's save and we'll run.
04:01When I scroll down to the bottom, you'll see here are the buttons that I have
04:04added with the icons.
04:05Here is the Up button, and the Down button, and left and right, and you can see
04:10that they've got their icons in the position where I've set them, and then
04:14here's the notext option down here.
04:17You can see that since I specified notext on the Delete button, and it's got the
04:20delete icon, only the icon is showing up.
04:23The last thing I want to show you is how to work with buttons programmatically.
04:28There are a couple of different functions you can call on buttons.
04:32Now, the way that you need to do this and we'll get to this a little bit later
04:36when we talk about events in jQuery, normally in jQuery, you would bind to the
04:41document ready event or the DOM ready event.
04:45And in jQuery Mobile because pages are parts of HTML files, you don't want to
04:51use the DOM ready because it's only going to fire once.
04:53What you want to do is bind to the pageinit function, which will tell you when a
04:58page is initialized, and that is when you can run any initialization code that
05:03you want to affect the page.
05:05And again, we'll get to this later on, so for now, I don't expect you to know or
05:09understand this completely, but just follow along with me.
05:11So I'm going to copy this, I am going to put this script down here in the bottom
05:17of the page, and what's going to happen is on the first page in the document
05:23here, I'm going to call the live function, and if you're not familiar with what
05:27I'm doing here with the live function or how I'm selecting the first page using
05:31the jQuery selector, then you need to go review the jQuery title here on
05:36lynda.com which explains everything that you see that's going on here.
05:39But, in a nutshell, what I'm doing is, I'm binding to the page initialization
05:43event, and I'm running a function when the page initializes to get the button
05:49that has an ID of Button 1, and I'm calling the disable method on the button.
05:55So what's going to happen is when the page loads, here is button with the ID
06:00btn1, it's going to disable this button element.
06:03So right now, let's just go back to the browser and you can see that right now
06:07that button is active.
06:08When I hover over it, when I click on it, it highlights in certain way.
06:12So what I'm going to do is go back in here, make sure that my script looks
06:17right, save it and then I'm going to run this again.
06:21You can see that when the page loaded, that script got executed and now
06:24the button is disabled.
06:26So now it's not getting any highlighting, functions, I can't click on it.
06:30So you can programmatically work with buttons as well as declaratively in
06:35the markup.
Collapse this transcript
4. Creating jQuery Mobile Forms
Understanding form basics
00:00One of the places where jQuery Mobile really comes in handy is in the
00:04creation of mobile forms.
00:06jQuery Mobile automatically enhances the form elements in a jQuery Mobile
00:12webpage to look really nice and professional and have nice styling kind of like
00:18native applications do.
00:20Now, there are a couple of things you need to remember about jQuery Mobile forms.
00:23First, all the form controls should be wrapped in a valid form tag;
00:27you can't have form controls that are outside of forms.
00:30You got to have the controls inside a valid form tag for jQuery Mobile's
00:34enhancement to work properly.
00:35The other thing is that all form controls have to have unique ID attributes
00:39across the entire site, not just the page that they're currently in, and the
00:43reason for this is because you have to remember that jQuery Mobile is doing
00:46things all the time like loading pages via AJAX and then sorting them into the
00:51DOM so that things work correctly.
00:53You should make sure that any ID attributes you have assigned to the form
00:57controls in your form are unique not just within their containing page, but
01:03across the entire site as well.
01:05The submission of forms is automatically handled via AJAX.
01:10So jQuery Mobile when the form goes to be submitted will intercept that process
01:16and submit the form using its AJAX engine.
01:20Now, I want to emphasize here, there's nothing special that you need to do on
01:25your server end in order to handle form submissions from jQuery Mobile, you can
01:30just go ahead and use the form submission logic that you're already using.
01:34In fact, it's pretty much invisible to you;
01:36you just simply define the form like you normally would, you define the method
01:40that the form is going to submit via either get or post, you define the handler
01:46script and everything should just work normally.
01:49To group related controls together in a form, you use a data-role attribute
01:55named fieldcontain, and this is a little bit different than the control group
02:00data-role that we talked about earlier.
02:02In this case, the fieldcontain takes a label associated with a form control and
02:08lays it out properly based upon the width of the page.
02:12So when the width of the page is greater than 480 pixels, it will automatically
02:17place the label next to the form control on the same line.
02:21But, when the width of the window is 480 pixels or less, it will automatically
02:26fall back to placing the label above the form field in the form, which is a nice
02:33little layout helper that jQuery Mobile gives you.
02:36But, you have to wrap the related label and form control in a container element,
02:41usually a div, named fieldcontain or with a data-role attribute of fieldcontain.
02:46jQuery Mobile now requires that controls have label tags, and the reason why
02:52this requirement is there is for accessibility.
02:55Now, there are times when you don't want the label to be usable and in order to
02:59make that happen, jQuery Mobile gives you a CSS class called
03:03ui-hidden-accessible, which you can apply to the label which will hide the
03:09label and you can use whatever placeholder text you want inside the form
03:13control, which if you're in a situation where you don't want the label to be
03:17visible, then that's fine.
03:19So let's take a look at some of these form basics in action.
03:23We're going to jump over to my code editor, and I've got my Snippets opened up
03:27to my Form Basics section and I have my formbasics_start page open.
03:32So let's take a look at some of the concepts we've discussed so far and see them in action.
03:37So the first thing we're going to do is copy the form itself, and I'm going to
03:41copy this guy right here, and paste it into the content area, and just close off that form.
03:50So let's take a label, and an input field, and paste it in, and we'll put that
03:56here inside the form.
03:57So what I've done is put in a label for this item here called the input.
04:04You can see that has the same name as the for attribute on the label here and
04:09it's got some placeholder text right here.
04:12So the nice thing about jQuery Mobile is that jQuery Mobile will implement a
04:17lot of the HTML5 form attributes for you even in browsers that don't officially support them yet.
04:22It's a nice little feature.
04:23In browsers that support HTML5 form features, they're just handled natively, but
04:28jQuery Mobile will fill in those browsers that don't support them.
04:31So let's run what we currently have, and you can see here I've got my label with
04:36my placeholder text.
04:37So far, everything looks fine, but notice that when I make the window larger,
04:41they stay on separate lines and the input field gets really long and what you
04:47really want to have happen is have the label on the same line as the text.
04:51We'll take a look in a little bit on how to do that.
04:53Let's go back to the Snippets.
04:57Now, in this case, what I'm going to do is copy in another label and input.
05:03Only in this case, I have the class of ui-hidden-accessible defined, so that
05:11in this case, the label won't be visible, but the input field will take up the entire line.
05:19So let's save this, and run it.
05:23So here you can see here's the first label with its associated input field, and
05:27in the second case, only the input field is visible, the label is not visible
05:31because it's being hidden.
05:32It's still there, so screen readers and other assistive technologies that will
05:37help users with those disabilities can still get the content of the form label,
05:42but it's just not visible on the screen right now. All right.
05:45Next, let's take a look at what fieldcontain does.
05:48So I'm going to copy those lines, go back to my form.
05:54Now, what I've done here is defined a container div with a data-role attribute
06:00named fieldcontain, and in this case, I've got a label and an input just like I
06:05have in the two examples above here, and this one has a setting called Your
06:09Address, and the placeholder text here is Your Address.
06:12So let's see how that works.
06:14We're going to save, we're going to run and you can see that when the window has
06:17a certain width, the label, and the field appear on the same line.
06:22But, when the window gets down to about 480 pixels, you can see that what's
06:27happening is the label is going on its own line, and the input field is going
06:32on the line below it, and the field container is putting a nestled divider line
06:37between the bottom of this little control set here and the next element in the page.
06:43If you want to combine the ability to hide the label with the field container,
06:48the way that you do that is by using the class of ui-hide-label.
06:53Now, this is slightly different than the ui-hidden-accessible.
06:58I am going to put that right on the field container.
07:02So now, same thing, only in this case, the label will be hidden.
07:05So when I run this again, you can see that the same behavior is there.
07:10So the field container is still putting this little divider line there, and the
07:15field grows with the width of the window, only now in the case of the input
07:19element above it, both those labels are now hidden.
07:23Okay, so that gives a pretty good introduction to the basics of form handling.
07:27Let's move into some more complex form elements now.
Collapse this transcript
Using text and search inputs
00:00jQuery Mobile supports basic text inputs and the newer HTML5 ones as well.
00:06So as you might expect, you can do things like this where you have input
00:09type="text", and of course, jQuery Mobile requires to have labels to go
00:14along with your inputs.
00:15But you can also use the newer HTML5 input types, things like number, things
00:20like email, and all the rest of them, URL and range, and all that stuff as well.
00:25They're all supported and jQuery Mobile supports the HTML5 attributes on those
00:30input types as well.
00:31In browsers that support it natively, that's not a big deal, but in browsers
00:35that don't support it natively, it's a really nice feature.
00:38Now in desktop browsers that's not a particularly useful feature because you
00:42have a full keyboard at your disposal and you can enter data really easily.
00:46Where this really comes in handy is when you're running your Web application on
00:50a mobile device like an iPhone or an Android and the Browser is smart enough to
00:55look at the input type and switch the virtual onscreen keyboard to match the
01:00kind of input that you're about to enter.
01:02There's also a supported input type of search and we'll take a look at that in a little bit.
01:08This is an input type that has a nice little UI that's specifically tailored for
01:13search terms and textarea controls are supported as well.
01:17And a nice little feature of textarea controls is that as you type text in them,
01:22they will automatically grow larger and larger in order to avoid scrollbars
01:26appearing which can be really hard to use on mobile devices.
01:30So let's jump over to the code and see how text and search inputs work in jQuery Mobile.
01:36So here in my Editor I've got my Snippets down on Text and Search Inputs and
01:41I've got my inputs_start file open and I'm going to go ahead and start trying
01:46some of these things out.
01:48So the first thing I'm going to do is get my form into the page and I'll put the
01:51form right here and just close that off, alright.
01:55Now I'll go back to the Snippets.
01:58And in this case, my field containers are not going to be divs.
02:02I'm actually going to use a fieldset in this case.
02:04Now again, you can use fieldset, you can use divs, whatever container you want to use.
02:09The nice thing about fieldsets is that they support using legends which we'll
02:12probably see a little bit later.
02:14So the first thing I'm going to do is copy the first fieldset in here into my form.
02:20And in here you can see that I'm using various different input types.
02:24I've got address right here, I've got a number, I've got an email, I've got a
02:29date right here, and actually, oops, I've got to fix that right there.
02:33So these are various input types, some of them are basic input types, a couple
02:38of them are HTML5 input types.
02:41Well, let's just go ahead and run this and see what happens in the Browser.
02:44So you can see here when I bring up the form, the address field gets a regular
02:49text input but my age right here is a numerical input.
02:55And Safari is showing me these up-down counters that I can use to enter numbers with.
03:00My e-mail address input field right here is a typical email field, and again,
03:05if we run this on an iPhone, you would see the virtual onscreen keyboard
03:08switch to email input.
03:09And here's a date input, and when I use a date input, I get these up-down
03:15counters and I can use this to enter a date instead of a digit number.
03:20Well, that's pretty cool.
03:22All right, so far so good.
03:24Let's go back to the code, let's try some other things.
03:27I'm going to copy this one right here, this is a Search Box.
03:34So we'll put this down below the other fieldset but inside the form.
03:38All right, so in this case we're using an input type of search, and ordinarily,
03:46without any enhancement this would just look like any other text input.
03:50But in jQuery Mobile it gets enhanced to look a little bit different.
03:55So you can see that the search input has a little Search icon with some
03:59automatic placeholder text that says Search the site.
04:02And when I put the focus in there and start to type a search term, you can see
04:06that little Delete button labeled Clear Text appears and I can use that to clear
04:12out whatever I've got currently entered into that field.
04:15So again, jQuery Mobile is enhancing the field to look like a Search Box that
04:21looks native to the platform.
04:23You can tell that the corners are a little bit more rounded than some of the
04:26other input fields are.
04:27So it's just a nice little visual cue that this is a searching input field.
04:32All right, let's go back to the code and in the Snippets, I'll go down to the
04:38next example and this is the last one and this is a textarea.
04:44So we'll put the text area in the form here and we'll go back to the Browser.
04:50So now in the case of the text area, it starts out being two lines tall, but
04:56you'll see that as I type text in here, I'll put in a line in, now watch what
05:02happens when I put in the third line.
05:04You'll see that the text area grows automatically, and that's to avoid any
05:09scrollbars from popping up.
05:12And you can see that as I enter more text, it grows by two lines each time.
05:16So again, that's the behavior that you just get by default of using jQuery Mobile.
05:20You don't have to do any special programming to get any of the features that
05:23you've seen here in this example.
05:24jQuery Mobile just automatically enhances your mobile forms to look really good
05:30on the Browser that you're using for your particular device.
Collapse this transcript
Using radio buttons, checkboxes, and toggles
00:00As you would expect, jQuery Mobile supports radio buttons and checkboxes and
00:05introduces a new kind of control called a Toggle Switch.
00:08Now jQuery Mobile of course uses the standard markup for radio buttons and
00:13checkboxes, but it enhances them on mobile devices.
00:17And a specially named select list can be styled to behave as a Toggle Switch.
00:24And you've seen these toggle switches on various mobile devices before.
00:28They're basically a binary switch that can be flipped into one of two different
00:33positions, either On or Off or however they happen to be labeled.
00:37Controls can also be grouped together in vertical and horizontal layouts.
00:43So just like you can do with buttons, you can do the same thing with radio
00:46buttons and checkboxes.
00:48So let's take a look at the code and see how this works.
00:52So here in my Code Editor I've got my Snippets open and I'm down in the
00:55Radiobuttons and Checkboxes section and I've got my radioschecks_start file and
01:01my toggle_start file open.
01:03Let's go ahead and start actually with the Toggle Switch, so I'm just going to
01:08scroll down a little bit to the Toggles section here.
01:11And let's go over to the toggle_start.
01:14Here this is the empty content section which is where our form is going to go.
01:19So I'll go back over my Snippets and I'm just going to copy these lines right
01:23here and paste them in.
01:27So what I have here is my form, and in my form I have a fieldset which has a
01:28data-role attribute of fieldcontain on it.
02:00And that's because I'm grouping this label with this select list.
02:01Now you'll notice that the select list is a little bit special.
02:02First, it only has two options in it.
02:03There's the first option and the second option and these two options represent
02:04the two different states that the Toggle Switch can be in at any given time.
02:05There is also a data-role attribute on the select tag named slider.
02:07And this is the special flag that tells jQuery Mobile that this is actually not
02:09a select menu, it's a Toggle Switch.
02:12So when jQuery Mobile enhances this control, it's going to draw it differently
02:18than a select menu normally would be.
02:21So let's save and see how this works.
02:23So I'm going to run this and you can see that when I run it, the Toggle Switch,
02:30first here's the label and here's the switch, right, so here's the On state and
02:34there's the Off state.
02:36So it's a two-state switch that can be either On or Off.
02:40And because I'm using the fieldcontain, you can see that as the window gets
02:44wider, the control goes on the same line as its label or it goes on the
02:50line below the label.
02:52In this case, this is the only control in the page, so there's not much else to
02:56separate it, but that's the purpose of using the fieldcontain right there.
03:01So that's how you make a Toggle Switch.
03:03Okay, let's go back and take a look at Radiobuttons and Checkboxes.
03:07So I'm going to close this.
03:08All right, over here in my radios and checks file, you can see down here I've
03:14got my div where the content is going to go, so we'll scroll back up.
03:19And the first thing I'll do is put my form in here along with my first fieldset
03:25and these are going to be the radio buttons.
03:28So I'll copy that and we'll paste that in and we'll close off the form.
03:34So now I've got my form and I've got my fieldset.
03:39And inside the fieldset, first I have a data-role attribute on the
03:43fieldset named controlgroup.
03:45So we've seen this before with buttons, but now I'm using it with the radio
03:48buttons and we'll see the effect of that in a moment.
03:51And I've also got a legend inside the fieldset which is going to provide a label
03:58for the Radio Button group.
04:01Now of course each one of the radio button inputs has to have an associated
04:06label to go along with the radio button for accessibility purposes.
04:10But let's watch what happens when this actually gets drawn in the Browser, so
04:16I'm going to run this.
04:18And you'll see that the radio buttons are being styled a particular way by jQuery Mobile.
04:23Here's the content of that legend tag right here, so that's serving as an
04:27overall label for the Radio Button group.
04:30And the label content of each one of the Radio Button tags, so you see here is a
04:36radio button right here and here's the label that goes along with it, the label
04:40is being converted into a part of the radio button itself.
04:44So here's the actual radio button, but you can see that the label has been
04:47turned into a clickable element which makes the accessibility and usability on a
04:53mobile device much easier because I'm giving my finger a nice larger hit target.
04:58So jQuery Mobile is incorporating the content of that label tag into the way
05:03it's rendering the radio button. So let's move on.
05:08Let's now get our next fieldset here, and these again are going to be radio
05:14buttons, but in this case, the control group, let me paste it in so you can see.
05:20So here you can see that the control group has a data-type attribute of horizontal.
05:26So rather than being vertical, these radio buttons are now going to be horizontal.
05:31So let's run this and you can see here that I have radio button behavior on these buttons.
05:38And because of the controlgroup setting, you can see that only the outside
05:42ones have the rounded corners, this is nice and flush, as are these here in the controlgroup.
05:47So here I have a radio behavior on a horizontally grouped set of buttons.
05:52All right, let's move on.
05:55Next I'm going to show checkboxes.
05:58In this case, I'm going to copy that and paste it down here at the end of the form.
06:06So once again, I have a fieldset with a data-role of controlgroup and I'm
06:10grouping together this legend and this label and this checkbox.
06:14So let's save that and let's run it, and you can see just like the radio
06:19buttons, the label is being incorporated into the control itself.
06:23The same is true with the checkbox.
06:25And you can see that this is the content of the label, this is the legend right
06:29here, and here's the actual checkbox by itself.
06:32So jQuery Mobile is enhancing these controls pretty well.
06:36And then last but not least, let's take a look at a group of checkboxes.
06:41I'll copy and paste that down here.
06:47So in this case I have a control group of many different checkboxes.
06:52I've got three checkboxes in here and I'll run that and we'll scroll down and
06:57you can see that we're getting the same kind of rendering as the radio buttons.
07:00Only in this case we have checkboxes which are multiple select instead of radio
07:05buttons in which only a single selection can be made.
07:08So using jQuery Mobile, we're able to create control groups using radio buttons
07:13and checkboxes and toggle switches that not just look really good, but are using
07:18all the contents of the different elements used to specify the control group to
07:23really create a nice, highly- usable experience on mobile devices.
Collapse this transcript
Using select lists and sliders
00:00All right. Let's finish up this section by taking a look at using sliders and select menus
00:04in our jQuery Mobile forms.
00:06To create a slider Control, you basically use a standard input tag with a type of range.
00:13And this is one of the new HTML5 input types that some of the newer, more
00:19modern browsers support.
00:21But for those that don't support it, jQuery Mobile provides an implementation.
00:25And in fact, even in those browsers that do support it, the implementation is
00:30styled to look consistent across different platforms.
00:33So the way to use a slider is something like this.
00:36You have an input tag with a type of range and then you specify values like the
00:41current value of the control is the value attribute.
00:44And then you have min and max attributes which represent the minimum and maximum
00:50value of the slider Control.
00:51And of course, there is a label that goes along with it and everything is
00:54wrapped inside a fieldcontain so that the label appears next to the control or
01:00above it based upon the width of the window.
01:03Select menus are a little bit different.
01:04You specify them as you usually do.
01:06You have your select tag like this and then inside the select tag, you have options.
01:10And of course, jQuery Mobile respects values like disabled and whatever else you
01:15have inside your select list which are standard HTML settings.
01:19And of course, there is a label that goes along with it and we wrap
01:22everything in a fieldcontain.
01:25Now by default, jQuery Mobile will provide whatever the platform's native select
01:31menu implementation is for selecting values from the list.
01:36So for example, on the iPhone, when you tap on the select list, that
01:40little spinner control comes up and you can spin to the different values
01:43and then select a value.
01:45On Android it's a little bit different, on Windows phone it's a little bit different.
01:49But by default, that's the behavior that you get.
01:52The onscreen appearance of the select menu is consistent across platforms,
01:58but the actual process of selecting the value is specific to however that platform does it.
02:04Now you can change that if you want to.
02:06jQuery Mobile can create customized select menus in which the selection process
02:13is the same across platforms.
02:15So why would you want to do this?
02:17Well, in jQuery Mobile's version of the selection UI, it supports multiple
02:23selection and it supports option groups which are kind of nice.
02:26It also allows you to style the selection menu with CSS.
02:32So if that's important to you, then you can use a customized select menu to do so.
02:36But probably the most important reason is that this ensures consistent
02:40behavior across platforms.
02:42Different mobile browsers have different implementations for selecting
02:47values from select lists.
02:48And in the cases of some browsers versus others, not all of the desktop features
02:53are always present and some things are supported and some things are not.
02:57So using a custom select menu in jQuery Mobile we'll ensure that the experience
03:02is the same across all platforms that your Web application is being run on.
03:07Now the way you do this, well there is one of two ways, for a particular select
03:11menu, you can use the data attribute called data-native-menu="false", which in
03:18this case will simply say, hey jQuery Mobile, for this particular select menu
03:22use your customized selection algorithm.
03:26If you want to apply this behavior globally to all of the select lists in your
03:31site, then you can bind to the mobileinit event that jQuery Mobile sends out
03:37when it's initializing and set the global property, which you see here, it's
03:42nativemenu and it's buried deep inside the selectmenu.prototype to be false.
03:47Now that particular technique is probably not familiar to you just yet and we'll
03:51get to using the mobileinit event later on in the course.
03:55But this is the technique you'll use when you want to set a global property
03:59across jQuery Mobile.
04:01All right, so let's jump over the code and see select menus and sliders in action.
04:06So over here in the code, I've got my Snippets and my Snippets are scrolled down
04:11to Select Lists and sliders and I've got my different select_start files open
04:16along with my slider_start file.
04:18So we're going to start with the slider, so let's create a slider control.
04:22What I'm going to do here is in my Snippets, I'm going to copy this code right
04:27here and paste it in and I'm going to put in the content section.
04:34So here I've got my fieldset which is my field container and inside there I've
04:39got my legend which is going to serve as the label.
04:42And here I've got my label that goes along with my range input right here and
04:48you can see that I've got a current value of 25 and it goes from 0 up to a 100.
04:53So I'm going to save this, I'm going to run it.
04:56When we run this, you can see that here's my slider Control right here.
05:02This is the slider portion, here is the associated text input field.
05:06And you can see that as I'm moving the slider Control, the number is updating in the Edit field.
05:12I can also type a number in there, like say 50, and you can see that as I'm
05:17typing the number in, the slider is updating in real-time.
05:22I can also use the Up/Down arrows to change the number and then when it loses
05:27focus, you can see that it updates.
05:30I can also use the keyboard.
05:31So here I'm using the Left and Right arrows and the Up and the Down arrows and I
05:38can use the Home and End keys to go to min and max, so lots of different ways
05:44for interacting with this control.
05:46So that's how you build a slider, let's take a look at the select menus.
05:51Let's start off with the basic select list.
05:53I'm going to move over to my selectbasic_start file and I'll scroll to the
05:58basic version here.
05:59Okay, we'll copy this and we'll paste it in.
06:07Okay, so here's my label and this is the basic select menu and you can see
06:12here I have a select and it's got a name and id, it's got some options in
06:16here, one of those options is disabled, each one of those has a value along
06:21with a text representation.
06:23So let's save and run.
06:25So you can see that jQuery Mobile has progressively enhanced the appearance of
06:31the select list here in the Browser, but when I tap on it, you can see that I'm
06:36getting the platform native pop-up menu experience.
06:40Now here on the desktop in Safari, I'm getting a regular select list.
06:44On a mobile device, this would look a little bit different, and you can see here
06:47that value is disabled so I can't select it.
06:50And when I choose a value, that value gets updated inside the control.
06:55All right, so that's a basic version of the select control.
06:59Let's take a look at a more advanced version, let's take a look at the selectgroup.
07:05So I'll move over to this one and in the Snippets, we'll go down to the Group.
07:10Now why would you use a Group?
07:12Well, let's take a look.
07:14I'm going to copy all this in here and I'm going to paste it in.
07:22So what I've got here is a form with a div in here and the div is assigned a
07:32data-role of the fieldcontain.
07:34And then inside that I've got a fieldset which is a controlgroup and it's
07:39specified to be vertical.
07:41So I've got two select lists in here.
07:44Here's one which contains a list of months and here's another which contains a list of years.
07:50So before I explain this any further, let's just take a look at this in the Browser.
07:53So you can imagine that this might be the kind of experience that you might see
07:58in an application that's asking you say for an expiration date on a credit card.
08:03So here we've got a way to select the month and we have a way to select the year.
08:08So I've got two different select lists that are grouped together.
08:11And you can see that because they're in a control group, the edges are being
08:15styled correctly so that they're flushed against each other, only the tops and
08:18bottoms have the curled edges and they work together.
08:21They're nice and visually grouped with each other.
08:23So let's go back over to the code, let's change this to horizontal.
08:28All right, now let's run this again.
08:33And now you can see that the experience is a little bit different.
08:35So when I make the window larger, because of the fieldcontain behavior I get the
08:40label on the same line, but when I make it smaller, it goes to different lines.
08:44And again, this is just a nice way of providing an experience to the user that
08:48groups two related select lists with each other, so pretty neat.
08:55Let's take a look now at our last example and that's going to be the
09:00customized select menu.
09:03So we'll jump over the start version of that and we'll scroll down in here
09:07to the Custom version.
09:10Now in this case, a couple different things going on.
09:17So I'm going to select all those in the form and paste them in down here.
09:25So I've got two different fieldsets in here.
09:29So here's my form and here's fieldset number one and this has the role
09:33of fieldcontain in it.
09:34Now there's no control group, it's just one select menu in here.
09:38And you can see that a couple of different things are going on.
09:40First, I'm using the data-native-menu="false" to indicate to jQuery Mobile that
09:46this should use the customized value selection process.
09:51I've also got options in here as well.
09:53So I've got an option group (optgroup) here labeled First Group and down here
09:57I've got an option group labeled Second Group, and inside the groups, I've got
10:01different option values.
10:03So before I explain it any further, let's just actually see what's going on.
10:06We'll run this in the Browser.
10:08So here's my Custom Select menu and when I click on the Choose One, notice how I
10:12get the customized value selection experience now.
10:16This is no longer the native menu that's being popped up.
10:19Now when I choose a value, I'm doing it using the customized selection
10:24experience, which is kind of nice.
10:26So a couple things to notice.
10:27Here's the option groups, right here.
10:29The option groups are being styled as these group labels and you can see that
10:32they're not selectable.
10:33When I click on those guys, nothing happens.
10:35The only time something happens is when I select a value.
10:38The other thing you notice is that here there's a Choose One label up at the top.
10:43And if you look back in the code, you'll notice that that's this option right here.
10:48Well, how did jQuery Mobile know to use this option as a title rather than
10:55a selectable option?
10:57The reason is because jQuery Mobile looks for option tags that have
11:00certain properties.
11:02If you for example, choose to create an option tag that has no value attribute,
11:08then that signals jQuery Mobile that this should be used a label.
11:11Similarly, if I provided no text but just said (value= that) and I ran this, once
11:20again, same idea, only now you'll see that when it gets run, there's no default
11:24value selected here because there's no value inside this option tag.
11:29And similarly, if I just simply leave the thing entirely empty, just an empty
11:34option altogether, once again, there's no default option provided.
11:39So let's undo that.
11:43And the other thing you notice is that the option group's labels right here are
11:48what's being used as the labels inside those highlighted group labels inside
11:53the UI, right here.
11:56Let's take a look at the second one.
11:58In this case, we're enabling multiple selections, so I can select more than one thing.
12:04And up here is the label and there's a little Close box that I can use when I'm
12:07done making my selections.
12:09And you can see that when I make selections, the values appear in the label of
12:14the control along with a Count Bubble indicating how many options I've chosen.
12:18So if I only choose two, then Count Bubble changes to 2.
12:23If I choose a whole bunch, then it fits as many values as it can but shows the
12:28number of selections in the Count Bubble.
12:31So let's go back to the code and see how I implemented that.
12:34Basically, in this case, I have a label called Multiple Enabled.
12:38And right here I have my option with only text inside of it.
12:42That's what's serving as the label at the top of the customized experience.
12:46And I've got multiple="multiple" set on my select list.
12:51That's standard HTML, that's not a jQuery Mobile feature.
12:54jQuery Mobile just recognizes that and sets the select list to be
12:58multiple selection enabled.
13:00And once again, I'm using data-native- menu="false" to trigger the customized
13:05selection experience rather than the native menu.
13:08And by setting all those options, I'm able to get a select list that provides me
13:13with multiple selection.
13:16That wraps up the coverage of jQuery Mobile form features.
13:20You can see that using jQuery Mobile, you can get some pretty advanced form
13:23features just using standard HTML markup.
Collapse this transcript
5. Working with List Views
Overview of list views
00:00In this section, we are going to talk about list views and list view controls.
00:04List Views are probably one of the more popular ways of displaying information
00:09on mobile devices either in native applications or in webpages.
00:14And jQuery Mobile provides a very powerful framework for marking up information
00:19and displaying it in a List View.
00:21So before we actually get started building our own list views, and seeing how
00:25the markup works and so on, what I am going to do is take a little bit of a tour
00:30through what list views can do on jQuery Mobile and I thought there's no better
00:34place to do that than on their own website, because they have a pretty good
00:38gallery of examples of what list views can do.
00:41So here I am on jquerymobile.com and I am going to start off by clicking on the Docs section.
00:46And when you go to the Docs section, the first thing you notice is we're greeted
00:50with a couple of list views.
00:52On the left-hand side we have a list view that's styled a certain way, and we've
00:56got various categories here for the different parts of jQuery Mobile.
01:00Over here, we have some list views, and you can see that this is a List View
01:04that has some divider items.
01:05This is a heading called Components, down here is another non-clickable divider
01:11that's called API, and you can see that as I move the mouse over each one of
01:15these items, it lights up and there is some click behavior that happens on it.
01:21It gets highlights when I click on it.
01:23There's a little standard right-pointing arrow which is an indicator that when I
01:27click on it, something is going to happen.
01:29So using jQuery Mobile, you can build these kinds of list view controls for your
01:34own mobile web applications.
01:36So to see what kinds of list views are available, I am going to go ahead and
01:39click on the List views item here in the documentation for the API, and that
01:45will take us to the Lists section.
01:48If I scroll down this page, you will notice that there are a lot of options in here.
01:51There are all kinds of examples of things you can do with list views.
01:55So let's just start off by taking a look at the most basic list, which is the
02:00Basic linked list right here.
02:02So you can see that this is the Basic linked list, and as I scroll down, there
02:06is nothing really all that advanced about it.
02:08Each item has a piece of text.
02:11There is an arrow over here and each one of these is clickable.
02:14Now, creating a Basic list like this is pretty simple and we will see that a
02:17little bit later, but for now let's look at some other lists.
02:20Let's take a look at the Numbered list.
02:22So this is the same as the Basic list, but you can see that now there are
02:26numbers next to the items.
02:29Same idea though, clickable items, right-facing arrow, only difference that
02:33there is numbers over here.
02:34Let's take a look at the dividers.
02:36Here is a list that has dividers in it, and you can see this is kind of like an
02:40Address Book application where you have the headers with the letters in them and
02:46then names underneath each one of those, and you can see that the dividers are
02:50styled a bit differently and they don't have the right-facing arrows.
02:54They are not clickable items.
02:55They are just serving as information separators.
02:59Now, let's keep on going.
03:01Let's take a look at the Count Bubble example.
03:03Now, you've probably seen examples like this before.
03:05You have got the list items, you have got your right-facing arrow, but here's
03:10a little Count Bubble that's inside the list item, and this basically shows
03:14you some additional information that's associated with that particular item
03:18that's in the list view.
03:20So in this example, we are looking at what appears to be an email application,
03:24and so we have categories for Inbox and Outbox and Sent and Trash and so on, and
03:29each one of those has a number associated with it.
03:31So in the Inbox, we've got 12 new email messages, and in the Trash, we've got 62
03:35items that need to be emptied, and we have sent 328 things.
03:38So you can easily build this kind of information into your list views when you
03:43build your app with jQuery Mobile.
03:44Let's get a little bit more advanced.
03:46Let's take a look at Nested lists.
03:48Nested Lists are lists that have lists inside them.
03:51So in this case, we have a list with three things;
03:54we've got Animals, Colors, and Vehicles, and you notice that when I click on the
03:57Animals, I'm taken to another page that has a sub-list in it, and if I click on
04:03Pets, you see I have got things like Canary, and Cat, and Dog.
04:06The idea here is that you have got lists that you can drill down into that
04:10contain more and more detailed information.
04:12So I am going to go back.
04:13Let's take a look at the Split button list.
04:17Now, the Split Button list is interesting.
04:19What it does is it divides the List item into two discrete areas.
04:23There is the main area here that's clickable, and you can see that there is some
04:26information in the main area of the list, sort of like a category.
04:30But, there is also another clickable item over here, and you can imagine that
04:34there's all kinds of scenarios where you might want to do this.
04:38For example, in this example here we are looking at what appears to be album
04:41covers and clicking on this part of that list item might take you to the details
04:46about that particular album, whereas clicking on this part of the button might
04:50take you to the purchase page and so on.
04:52So Split Buttons allow you to just the scenarios where you want to have two
04:56different clickable areas for two different actions on the same list item. All right.
05:01Let's keep going.
05:02Here are some icons, and in this case, we are looking at a list that has some
05:07icons along the left-hand side of the title part of the list item, and it also
05:14has some count bubbles and some arrows.
05:15So now we are getting into some scenarios where we are combining
05:17different features.
05:19Then, last one we are going to look at before we move on and start building our
05:21own list views is the Search Filter bar.
05:25So in this case, we have a list view, the same as the Basic when we saw earlier.
05:30But, in this case we have a filter up here at the top.
05:33So if I type items in here, you will see that the list starts to get narrowed as
05:39I type more and more information.
05:40So for example, if I type Ac, I get Acura and Cadillac.
05:44So these are different car models and so on.
05:46Again, these are all features that your list items can contain in your own applications.
05:53So now that we have seen a little bit about what List Views are, and what they
05:57can do, let's take a look at building our own list views and see how to mark
06:02them up to achieve the various features that we have seen here.
Collapse this transcript
Basic list views
00:00Let's begin by looking at how to build some Basic List View features.
00:05jQuery Mobile can enhance both ordered and unordered lists, and the way that you
00:09build these list views is use standard HTML Markup to define either an ordered
00:14or an unordered list and then you apply the data-role of listview to the list,
00:20and that's all you need to do to make a basic list view.
00:22jQuery Mobile, when it loads, will search the page, find lists that have this
00:27data role on them and then enhance them to look like the list views that we saw
00:31in the previous movie.
00:32Now to make items tappable, you have to enclose the List Item content inside link tags.
00:39If you don't do this then you will have a read-only list.
00:42You have a list that will have items in them that are for display-only but
00:45don't respond to taps.
00:47And to declare Divider items, you create those by using the data-role of divider
00:53on a particular list item.
00:55So that's pretty much all there is to it.
00:56To create a list view, all you do is create a list either ordered or unordered,
01:01apply the data-role of listview and then put some list items in there, and if
01:05we want the list items to be tappable and what I mean by tappable is they will
01:08have that little right-pointing arrow on them, then just simply put anchor or
01:13link tags inside your List Items, and to define a Divider just put the
01:17data-role of divider on your List Item, and don't put a link in there because
01:22you don't want it to be tappable.
01:23All right, let's continue.
01:24To define a Count Bubble like we saw earlier you simply put the number inside a
01:29span element or some other inline element that you can style and then add the
01:34class ui-li-count around the number like you see here.
01:39So in this case, I have got a list item whose text is going to be List Item 1
01:44with a number 15 in it and the number 15 is surrounded by a span with a class of
01:49ui-li-count and that will wrap the number inside that little circular bubble
01:54like we saw in the previous example.
01:56You can also specify Icons, and Icons can be specified by, including image
02:01inside link tag that has the class ui-li-icon applied to it.
02:07So here we have another example where I have got my link, and inside the link, I
02:10have my image which provides a path to my image, in this case, a PNG image.
02:15And then I saw in the class of ui-li- icon to it and that will mark up the icon
02:22centered in list item next to the text that makes up the tag.
02:26Okay, let's jump over the code and actually build some of this stuff.
02:28All right, so I am here in my code, and I have got my Snippets opened, and I
02:31have got basiclistview_start.
02:34So let's start with a Basic List View and you can see here that in my Basic list
02:38view inside my content section I have two lists.
02:42One of them is an Ordered List, one of them is an Unordered List and I am going
02:45to mark up each one of these to appear to be a List View.
02:48So I am going to do that is I am going to copy this Ordered List tag right here
02:53with the data-role of listview on it, and replace that one.
02:57I will do the same thing down here.
03:02But that's an Unordered List, so I will make that an unordered list, and I will save.
03:07Now before I do anything else, let me run this real quick, and you can see
03:10that jQuery Mobile has now enhanced the list so that in the top part, I have
03:15an Ordered List and I have an Unordered List down here, but notice how the
03:18items are not clickable.
03:19There are no arrows, there is no rollover behavior when I move the mouse over them.
03:23So these are just read-only list items.
03:25In order to make these tappable, I have to put links inside them.
03:28So let's back to the code.
03:31And what I am going to do is copy these guys right here and we will paste them in.
03:38Okay, so now I have got links and just ignore for the moment that they have an
03:44href of a Hash(#) character.
03:46jQuery Mobile ignores links have has # characters and then it doesn't actually
03:50do anything with them.
03:51But I'm only, including them so that they will be clickable items.
03:54So we will save and we will refresh and now you will notice the items are now
03:59styled in a fashion that shows that they are clickable.
04:03There's a little right facing arrow icon over here.
04:06You can see that the items are being styled with a nice gradient in both the
04:10Ordered and Unordered cases.
04:13So let's go back to the code and now let's make a list with some dividers in it.
04:18So I am going to open up listviewdivider_start, and we will go back to the
04:22Snippets and we will scroll down to the Divider section and all I need to do to
04:28make a list item a divider is put the data-role of divider on it.
04:32So I am going to copy that and in both of these lists, I am just going to insert
04:39some dividers where I want dividers to be.
04:41So I will put one here and I will put one there, and I will put one over
04:45here and I will save.
04:47And now we will run this, and you can see that I have got List View dividers
04:53that are now in my list.
04:54So here is a list view divider, you can see it's not clickable whereas these are.
04:59Up here in the Ordered List same thing, I have got dividers that are not clickable.
05:02All right. Let's continue on.
05:05Let's go back to the Snippets.
05:09Now we are going to do some Count Bubbles.
05:10So let's put some items with count bubbles into our list.
05:14I am going to open up the Countbubble_ start and what I am going to do is run it
05:19really quick so you can see.
05:20So here is the standard list that we have been working with so far.
05:23Here are all the items.
05:25Let's go to the Snippets code and let's copy these guys here and paste them into
05:32the file here and down in the Unordered List as well.
05:37So what have I done?
05:38Well, these are the same list items that they were there before but now they
05:42have a span inside them and each one of the span has a number and you can see
05:47that the span has a class of ui-li-count applied to it.
05:53So when I save, now let's run it again, and you can see that Count Bubbles have
05:59now appeared in the items where I happened to have those numbers.
06:04And on List Item 5 in both cases I don't have a count bubble, so nothing shows up.
06:09That's right here,
06:10there is no span, there is no class of li-count so nothing shows up there.
06:16Last but not least, in the Basic category, let's add some icons.
06:20So I am going to open up my listviewicons_start file right here and over in
06:28my Icons section in Snippets, I am going to copy and I am going to paste, and paste.
06:40So in this case, what I have got is text inside the item, so I have got United
06:46States, Great Britain, Germany and France.
06:48And I have got these images in an images directory inside the same folder as
06:54this example file pointing to the small little icons that will be styled with
07:00the ui-li-icon class which will place that image next to the text in the list.
07:07So I am going to save that, run it and you can see that in both cases now, in
07:13both the Ordered and Unordered Lists, I've got icons appearing next to my text.
07:19So that covers creating some Basic List View features,
07:22we will move now on to some more advanced stuff.
Collapse this transcript
Advanced list views
00:00Now that we have seen how to build some basic list view functionality, let's
00:03move on to some more advanced stuff.
00:05In fact, the first thing we are going to look at sounds advanced, but it's
00:08actually pretty simple and that is creating a list view filter.
00:12So, creating a list view filter is as simple as adding the data-filter attribute
00:16with a value of true onto the listview element.
00:20And you can see here I have got an ordered list that's specified as a listview
00:24and the data-filter is equal to true.
00:27You can even customize the placeholder text that's in the list view, if you use
00:31the data-filter-placeholder attribute and you put some text in there that
00:36represents what you want the placeholder text of the filter to be.
00:40You can also nest lists within other lists.
00:43So for example here I have got a listview and I have got a few List Items and
00:47then in my last List Item I've got another embedded listview and this will
00:52create the behavior where if you click on the item that's labeled More Items, in
00:57this case, the screen will animate to the left, it will do a transition and take
01:02you to the embedded list view.
01:04You can do this as many times as you like to drill down into more detailed lists.
01:09And of course, List Items can be split into separate tappable areas, we talked
01:14about this earlier, this is creating a split button and you simply add two link
01:18tags to the list item.
01:20So for list item that you want to be a split area, you put in a link that will
01:24be the large area and another link that will be the small icon area and jQuery
01:28Mobile is smart enough to use the first and the last links.
01:32If you put more than two links in here, jQuery Mobile won't get confused, it
01:35will use the first link for the large area and it will use the second link for
01:39the small icon area and any other links, you put in here, will just simply
01:43become content inside that particular list item. All right.
01:48So let's jump over to the code and see some of this in action.
01:51So here we are in my ExampleSnippets and I have got my listviewfilter_start,
01:57nestedlist_start, splitbutton_ start and listviewrich_start opened.
02:02The listviewrich example is one I didn't show on the slides, but it will show
02:06you how to do some rich formatting inside your list items.
02:10So let's begin by taking a look at the starting case of the listviewfilter.
02:15So if I just run this as it is, you'll see that I've got an ordered list and an
02:19unordered list and we want to apply some filtering to these guys.
02:21So let's go back to the code and let's replace the Ordered List definition with
02:26this one and if we just put data-filter ="true" on there, and we run it again,
02:32you will see that my little filter appears and it says Filter items and I can
02:36do things just like type some characters and you can see that as I'm typing,
02:40character's information is disappearing and I can click the clear button and so on.
02:46Now let's go back to code.
02:49Let's do one more thing.
02:51Let's put the data-filter-placeholder in here.
02:58So now we have a filter and I have a data-filter-placeholder and instead of
03:04saying Filter items, it says Filter Countries.
03:07And the one other thing that I am going to do is put an attribute on both of
03:13these called data-inset="true".
03:18Now you notice probably earlier that the lists went to the edge of the screen in
03:22both cases and that's because the way that lists are styled by default, they
03:27take up all the available width in the screen and by doing data-inset="true",
03:34let's see how that changes things.
03:36So I am going to save and then back here in the browser, we are going to refresh.
03:39So now watch the edges of the list here.
03:41So you can see that the list has now become inset into the page a little bit
03:46with rounded corners in the top and bottom elements and the items are flushed
03:51with each other and these guys are straight over here.
03:53So by insetting the list, you can have the list offset from the edges of
03:59its container element.
04:01This is useful when you want to put lists inside things like forms or
04:04collapsible content blocks or other container elements.
04:08And then the other thing you notice is that in the case of the filters, this one
04:12still says Filter items, this one now says Filter Countries.
04:16So my placeholder is now taking effect right here that's the effect of the
04:22data-filter-placeholder.
04:24Let's move on to the next example.
04:26Let's take a look at Nested Lists.
04:28So here's my Nested Lists starting point and over here in the code, I am going
04:32to copy my two lists.
04:38So here's my Ordered List and here's my own Unordered List.
04:45We are going to put that one down here.
04:49So notice what I am doing is in the case of this list, I have the
04:53data-role="listview" on it and over here I have got the data-role="listview" on a sub-list.
05:00So I am going to save this and I am going to run it.
05:05You can see that in this case, where I don't have any anchor tags or anything,
05:09these list items are not clickable, but this one is and when I do it, notice
05:12how I drilled down into the More Items detail, all right, same thing in Unordered List.
05:18So that's how you create nested list items.
05:21You can do this as many times as you want.
05:23You can have nests that list pretty deeply.
05:26That's Nested Lists. All right.
05:27Let's move on to the next example.
05:32Let's look at Split Buttons.
05:33In this case, I am actually going to use the Split Buttons with a Nested List.
05:40So I am going to copy this code right here, copy it and here's my starting point.
05:45Now before I get started, what I am going to do is just run this as it is
05:50and you can see that I have got a list view with some Wine Regions from
05:54America, France and Italy.
05:55If I click on America, you'll see this California, Washington, Oregon,
05:58right here is France.
06:01Here is what I'm going to do, I am going to copy and paste that Nested List with
06:04Split Buttons inside here.
06:06So let's go back to the code and inside the California list item, I'm going
06:13to put this nested, unordered list with list items that have two link tags
06:19inside each one of them.
06:20I am going to save that.
06:22Now before I go back and run this, I am going to go back to the Snippets and
06:27copy over the two different pages that those items link to and you can imagine
06:36that what I'm doing here is if you got an application that displays information
06:41about Wine Regions, you can imagine that there's a page where you read details
06:45about the wine and there's a page where maybe you enter your own tasting notes.
06:49So that's what we are building right here.
06:52I am going to scroll back up and save and we will run it again.
06:57Now when I click on America, now you will notice that I have a nested list there.
07:02So when I click on California, I'm getting this split list behavior.
07:06So here's the large area, this is the first link and you can see I have got 3 of
07:10these and then over here, are the Split Buttons.
07:13And even though, I gave a label for each one of these, what's happening is an
07:18icon with the arrow is showing up and the text that I provided in the anchor tag
07:24is now being listed as the little pop-up on the icon.
07:27Now obviously that won't happen on a mobile device, but on the desktop device, it does.
07:31So if I click on this item, it will take me to the Wine Details page, whereas if
07:36I click on this icon, it'll take me to the Tasting Notes page.
07:40So there are all kinds of common scenarios where you want a list item to have
07:45two different tappable areas.
07:47Imagine a movie application where this is the name of the movie and it shows you
07:51the information about the movie and this one is the one that purchases tickets.
07:54This scenario pops up all the time. All right.
07:57The last example I want to show you is Rich Formatting inside a list.
08:02So what I am going to do here in my listviewrich starting point, let me
08:07quickly run it and you can see I am starting off a blank page.
08:11But what I am going to do is go over to my Snippets and I am going to first copy
08:18over the Unordered List.
08:21So we'll put that in here and we'll close that off.
08:26And then I'm going to copy in the first couple of list items.
08:30So I will copy that and paste it over here, so you can see what's going on.
08:36So my first list item is a divider and it has a date inside of it.
08:43And then inside my list item, inside my link tag, I've got some pretty rich formatting.
08:48Here's an h4 heading, here's a paragraph with a style on it, here is another
08:53paragraph with a class assigned to it of ui-li-aside.
08:58Here is another paragraph with some text in it.
09:00So before we paste in the others, let's just run this and see what happens.
09:04So you can see that I've got an inset list and that's because this data-inset
09:08attribute right here.
09:10It's pretty richly formatted.
09:12First, here's that h4, here is that paragraph with the bold text, here is that
09:18other paragraph with the detailed text and that ui-li-aside class caused that
09:24other paragraph to be placed up here on the side of the list item, sort of like
09:28a little aside text.
09:30So let's go back to the Snippets and paste in the rest of these.
09:35Now that you see what's going on, it's kind of like a little to do sort
09:39of agenda application.
09:41I will copy these, paste them in and we'll go back to the browser and refresh it.
09:50Now you see that I have got a list of To Do Items inside my application and
09:57using the rich styling, it looks like you can get some pretty advanced
10:01formatting inside these list views, this is pretty close to what a native
10:05application might look like.
10:07These are all clickable.
10:08They don't do anything right now, but they are inside link tag, so they are
10:11getting the click behavior, you have the right arrow over here, you have the
10:14different formatting with different locations for text.
10:16So using jQuery Mobile list views, you can actually get some pretty
10:21advanced layouts going that begin to rival what native applications can do
10:25in their layouts.
Collapse this transcript
6. Using the Content Formatting Tools
Using layout grids
00:00In this section we are going to take a look at the various content formatting
00:03tools that jQuery Mobile provides for you to control how the content of your
00:08mobile webpages is laid out and presented, and we are going to start by taking a
00:12look at using Layout Grids.
00:15jQuery Mobile provides a set of layout grids from two the five columns and
00:19they're pretty basic, but they're pretty useful.
00:22They are useful for laying out smaller elements, think of things like Form
00:25Controls or icons where layout precision is needed.
00:29And a really good example of this is in footer toolbars, because recall from
00:33earlier that footer toolbars don't have the same kind of prescriptive layout
00:38guidance that header toolbars have.
00:40So layout grids can be useful for when you need to position things like
00:45form controls or multiple text fields in a footer toolbar and they need to
00:49be positioned just so.
00:51jQuery Mobile's layout grids do a really great job of that.
00:54They are based on a nested structure and any layout container can serve as the
01:00root element. Typically you'll use a div, something like this.
01:04What you'll do is you will define a class on that div or whatever the container
01:08element is of ui-grid followed by a letter.
01:13For the grids the letters go from a through d;
01:16and a corresponds to two columns, b corresponds to three columns, and so on up
01:21to d which corresponds to five columns.
01:24Now inside this div you will use other containers, again probably divs, and
01:28in this case you will put classes on these days named ui-block- and then
01:34followed by a letter.
01:35Now in this case for two columns because we are using ui-grid-a, we are going to
01:39have two divs inside the parent div and they are going to have classes of
01:42ui-block-a and ui-block-b.
01:45Then inside those divs that's where you will place your content.
01:48Now if you were using ui-grid-b, c, or d you would have more divs than you see
01:53here, but this is for a two column layout.
01:56The layout grids take up 100% of the available page width and they don't have
02:01any padding or margin applied to them which basically means they won't
02:04interfere with the layout sizing of any of the container elements that you put inside the grids.
02:11The grid columns are all equal widths and I have a table here that shows you
02:15what the column widths are for each one of the grid layout classes.
02:20So for ui-grid-a there is two columns and there are 50% and 50% of the
02:24screen width and so on down to ui-grid-d which has five columns each one of width is 20%.
02:30Grids can wrap to multiple rows.
02:33If you have a layout container;
02:34let's say you have ui-grid-a and inside you put two divs ui-block-a and ui-block-b,
02:41if you put two more also called ui- block-a and ui-block-b, then jQuery Mobile
02:46smart enough to realize that you got four divs inside a two column layout and
02:50it will wrap each one of those to their own line and you can do that as many times as you want.
02:55So if you wanted to have eight rows let's say you would say eight divs inside
02:59the parent of a ui-grid-a and then you have to assign ui-block-a and ui-block-b
03:04to each one of the divs in an alternating fashion.
03:08So let's take a look at the code and we will see how to take advantage of the layout grids.
03:14Here in the code I've got my ExampleSnippets open and I have got my grids_start open.
03:18This is the file we are going to be working on, but before we get started on
03:21that let me just fire up the finished version.
03:24I am just going to run this so you can see what it's going to look like.
03:27So here is the Layout Grids Example in the finished state.
03:30So you can see we have a two column layout and these are the two divs here
03:34and you've got block A and block B and then Three-column layout Four or Five columns.
03:38Here's an example showing multiple rows.
03:40This is a Three-column layout with two rows in it and then down here at the
03:44bottom I've got some buttons laid out in a layout grid showing how you can put
03:49Layout controls inside a layout grid.
03:52So this is the finished example let's close this and we will close this as well
03:56so we go back to the starting state.
03:57And now we are going to build that example.
03:59So let me scroll down here to my page and we'll go over to the snippets and we
04:04will start by just copying and pasting the code for the Two-column layout.
04:08So we will copy those lines right there and we will put them inside the
04:13content area right here.
04:15So we'll save and now let's just bring that in the browser and you can see
04:18that's how we have arrived at the two column layout.
04:21So let's go back to the code really quick and take a look.
04:23You can see here, here's the parent div with the class of ui-grid-a applied to
04:28it and then inside I have got ui-block-a and ui-block-b.
04:33Now if I just copied and pasted these two guys again like this and then ran, you
04:39can see that I am making multiple rows.
04:40We'll save that for later.
04:43Let's just go ahead and take those off.
04:45So let's go take a look at the rest of layouts.
04:47Here we have got three columns and we will copy that and paste that.
04:53And we will run it.
04:54Now you can see I have the three columns.
04:57Let's go back and paste the rest.
04:59So here's the Four and the Five one as well.
05:05And we will paste those in down here at the bottom and we will refresh the
05:08page and you can see now that we've got examples of all of the columns laid
05:14out here in our page.
05:16We have got Two, Three, Four, and Five columns.
05:18So you can see that you can use these columns to lay things out in a pretty
05:22good precise fashion.
05:23The Five-column layout is probably useful for positioning icons and smaller form controls.
05:28So let's close this.
05:29Let's just go back to the code.
05:30Now let's take a look at the multiple rows.
05:32In this case I'll use the Three- column layout with multiple rows.
05:36So we go ahead and paste that in.
05:38Now you can see here than using ui- grid-b and ui-grid-b is the Three-column
05:44layout because ui-grid-a is two.
05:46Inside here I'm using two sets of three divs.
05:50Here is the first set right here and you can see I have got ui-block-a and
05:55ui-block-b and ui-block-c.
05:57Then I repeat over again with ui-block- a and that tells jQuery Mobile that I'm
06:02wrapping to a new row in my layout grid.
06:06So if we save this and we run it you'll see that if I scroll down we have a
06:11three column layout with multiple rows in it and you can repeat this as many
06:14times as you want for as many rows as you need.
06:17So I will close that.
06:19Last but not least, let's show a couple of different things.
06:22First, we are going to show laying out form controls and we are going to show it
06:25using the layout grid class applied to a different kind of container.
06:31So I am going to copy this and I am going to paste this down at the bottom of
06:35my form and I could put this in the footer if I wanted to, I could put it in the content.
06:38Just for the sake of this example I am going to put it in the content, but you
06:41can use these layout grids in your footers or header or anywhere else you want
06:45to use a layout grid.
06:47So in this case I've got a form and I've got some buttons inside the form and
06:53again they don't do anything;
06:54I am just doing this to show you the layout.
06:56But I've got a field set, and in this case I'm assigning the class of the
07:00ui-grid to the fieldset tag instead of a div and the reason I am doing that is
07:06because I want to group my controls inside of fieldset and I want to use the
07:09legend that goes along with fieldset.
07:11So the legend says you know catastrophic failure action and this is a play on
07:15the old windows Abort, Retry, Ignore dialog.
07:18So what I have got here is three buttons laid out in a three column grid.
07:22So I am using grid b be with ui-block-a, b, and c. So that will create three
07:26columns and each button is getting its own column.
07:30So if I save this and run it, and scroll down, you can see that each of the
07:35buttons is taking up 33% width in a three column layout down here at the
07:40bottom of the page.
07:41So using Layout Grids you can achieve some really common and useful layout
07:45precision in your mobile webpages whether you're laying out icons or controls or
07:50blocks of text or anything.
07:52The Layout Grids in jQuery mobile can be pretty useful for those kind
07:55of scenarios.
Collapse this transcript
Using collapsible content blocks
00:00Another really great and useful content formatting tool that jQuery Mobile
00:04provides is the Collapsible Content block.
00:07And I'm sure you have seen these before on various websites you may have visited.
00:11These are content blocks that can be expanded and collapsed by the user by
00:16clicking on them and jQuery Mobile makes creating these really simple.
00:21Any container element can be designated as a collapsible content block and all
00:25you need to do in order to achieve that is apply the data-role of collapsible to
00:31the parent container that you want to be collapsible.
00:33So in this case here I have got a div tag and I'm giving it a role of being a
00:37collapsible content block.
00:39Now within the container any header elements will become the title and any of
00:43the following content will be wrapped inside an element that will become the
00:47collapsible content.
00:49So to declare a complete collapsible content block I would do something like this.
00:55First, I would have a div and I would give it a data-role of collapsible.
00:57Then inside the div I would give n h1 or an h2, h3, whatever all the way up to
01:03h6, whatever header tag I want to use, I can give the content for what the title
01:08of the collapsible content block will be, and then any of the content that
01:11follows the header will become the collapsible content.
01:14So in this case I have got a paragraph and this is the content that will be
01:17shown and hidden when the user clicks on it.
01:21Now by default collapsible content blocks are collapsed.
01:24When the page loads the content is not visible and the blocks are in collapse state.
01:29To prevent this from happening;
01:30in other words to have the collapsible blocks open by default, you simply use
01:35the data-collapsed attribute with a value of false.
01:39So in this case I have got a div and I am giving it a role of collapsible and
01:42then I am saying hey you actually start life out as an open collapsible block.
01:47Collapsible blocks can also be nested inside of each other.
01:51So for example I can have something like this where I have a div and I've got
01:55the collapsible data-role on their and inside I have got my title and my
01:59collapsible content, but then I can put another one of these inside.
02:02So I can have another div that's a collapsible content block with a heading and some content.
02:07You can do this as many times as you want and inside these collapsible content
02:11blocks you can pretty much put whatever you want.
02:13You can put forms in there, you can put list in there, you put any kind of
02:17markup that will be shown and hidden when the user clicks on it.
02:21So let's jump over to the code and actually build one of these.
02:24So here I am in the code and I have got my ExampleSnippets open and I have got
02:29my collapsibleblocks_start file open.
02:31This is the file that we are going to use to build our collapsible blocks.
02:34So over in the ExampleSnippets I scrolled down here to my collapsible blocks and
02:38we are going to just start off by copying the first example.
02:42So I am going to copy that code right there and paste it into my content section here.
02:47We will save it and we will run it.
02:52You can see that I have got a collapsible block, a little plus sign (+) right
02:55there, this is the title.
02:56I will click on it and you can see that the plus sign (+) changes to a minus
02:59sign (-) and the collapsible content is now visible and you can see I click on
03:05this and it shows and hides the content.
03:07Okay so let's go look at the code, you can see here, here's my parent div.
03:10This is the container element that's going to be the parent of the collapsible
03:13block and here's my heading.
03:15This is what the title is going to be.
03:17And you can see, I've given it in h1, but I could give it in anything, I can
03:20give it in h3 and let's save that and run it.
03:24And you see it doesn't really make any difference jQuery Mobile in this case has
03:27styled content to look the same size.
03:30So let's go ahead and back to the code and change that back.
03:35Here is the content that I want to be collapsible.
03:38This is the content that is shown and hidden and I can put whatever mark up in here I want.
03:41So let's go back and copy another example over.
03:44Let's try out the data-collapsed = false state.
03:49So we will paste this in and I need to paste that inside my Content section.
03:55Let's save and run, and you can see that this one is closed, but the one with
03:59the (data-collapsed=false) defaults to being open.
04:03So now it's open by default. Let's continue on.
04:06Let's get another example.
04:09Let's get a nested collapsible block right here.
04:13I am going to paste that in.
04:18Now here you see I have a nested collapsible block, I have got a parent div here
04:21that's collapsible, I have got another div here that's collapsible and in both
04:26cases I've got the heading and the content and the heading and the content.
04:30So let's save and let's run.
04:33Here's a nested block right here.
04:35You can see that when I expand and contract this in the expanded state here is
04:39the nested one and I can expand that one as well.
04:41You can see its remembering its state in between each one of the collapses.
04:48Let's go back and actually try something.
04:50Let's try this as (data- collapsed="false"). Let's save that.
04:59Let's run it again.
05:00Now you can see that in this case the nested collapsible block defaults to open
05:07whereas in the previous case it defaulted to close.
05:10So that's using collapsible blocks in jQuery Mobile to show and hide content
05:15to the user.
Collapse this transcript
Using collapsible sets
00:00A close relative of the collapsible block is the collapsible set and I am sure
00:04you've seen these on the web before.
00:06Sometimes they're called accordion controls, they maybe called something else.
00:10They're basically made up of individual collapsible blocks.
00:14Now, the difference here is that only one of these blocks can be opened at a
00:16time, and when user clicks on one to open it up, if another one is open,
00:21then that one closes.
00:22And the way that you define these in jQuery Mobile is really pretty simple.
00:26Instead of putting the data-role of collapsible in the parent container, you
00:31use the collapsible-set value and here you can see I am using a parent
00:34container of a div and I've got the data-role defined as collapsible set and
00:38then inside the collapsible set you put the collapsible blocks that you want to
00:42make up the collapsible set.
00:44So in this case I've got two collapsible blocks and one of them is using the
00:48data-collapsed attribute with a value of false two default two open.
00:53So in this case I have got two blocks and they are mutually exclusive, when one
00:57of them gets open, the other one gets closed and that's the basic behavior of a
01:02collapsible set or accordion control or whatever you want to call it.
01:05So let's quickly jump over to the code and build one of these and then we'll move on.
01:08Okay, so here in the code I have got my Snippets scrolled down to the
01:12collapsible set example and over here I've got my starting point for
01:16the collapsible sets.
01:17So let's go back to the snippets and let's copy this over and let's just begin
01:22by copying over the parent container.
01:25So what I am going to do is put that in there, close that div off.
01:28So here we have the heading, this is the header for the example and then here's
01:32the actual collapsible set itself.
01:34So this is going to be the parent container and you can see there's a data-role
01:37of collapsible set on here. All right.
01:40Let's go back to the Snippets, now let's copy the content, that will take up the
01:44collapsible blocks and we've got four of these blocks and we're going to paste
01:50these inside the collapsible set div.
01:53So now I have got a collapsible set with four collapsible blocks in them and the
01:57one up here on the top is using data- collapse="false" to be the default open
02:02case and you can see that I've got content and headings in each one of them.
02:06So let's save and let's run.
02:08So when we run it, you can see that Section 1 defaults to the open state and all
02:13the others are closed.
02:14You can also see that jQuery Mobile is doing a good job of CSS styling, so that
02:18only the ones in the top and the bottom have the rounded corners and the rest
02:22are all flushed with each other.
02:24So when I go to open Section 2, you can see that Section 1 closes and so on with
02:29the other sections and in fact I can close all of them if I want to.
02:32Let's go back to the code, let's take the data-collapse attribute off of this
02:39one and put it down let's say on number 3 here, so we'll have that one be open by default.
02:45Let's run again.
02:46You can see that this time section 3 is now the one that's open by default.
02:49So that's how you take collapsible blocks and group them together into a
02:54collapsible set or an accordion control in jQuery Mobile.
Collapse this transcript
Using themes
00:00One of the most powerful features of jQuery Mobile is its styling architecture.
00:04jQuery Mobile includes a really powerful styling architecture based on CSS3.
00:10It uses CSS3 for things like rounded corners and background, gradients, and
00:15shadows and all the things that up until now you would have had to use things
00:20like graphic elements in order to accomplish.
00:23So, relying on CSS3 means that jQuery Mobile doesn't need to download a whole
00:28bunch of heavyweight graphics in order to achieve some really nice styling and
00:32visual presentation.
00:34So basing things on CSS3 until recently would have been a problem as well for
00:38browsers, but because most modern mobile browsers now use CSS3 especially the
00:42WebKit ones, you can rely on the fact that CSS3 will be available in most of
00:48your users' browsers and your jQuery Mobile applications will look really well.
00:52And in fact using CSS3 in many of the modern browsers not just is a good idea
00:57for visual reasons, in many cases the CSS3 features like animations and so on
01:02are hardware accelerated.
01:03So the performance is much nicer and much faster than it would be if you were
01:08using old-school techniques like background images to achieve things like
01:12gradients and so on.
01:14Now, a set of unified visual appearances in jQuery Mobile is called a theme and
01:20jQuery Mobile ships with five of these, and each theme has multiple swatches
01:25what we call different color swatches inside the theme.
01:28Swatches are assigned letters starting with A and ending with Z. You can have up
01:32to 26 swatches per theme, and the nice thing about themes is that themes can be
01:37mixed and matched throughout different parts of a page.
01:41And the way that the jQuery Mobile theme architecture is built, they've
01:46gone ahead and done things like separate the notion of color from other
01:51forms of visual appearance.
01:52So you can use different themes on different parts of your page, and the way
01:57that it's built, the child elements of the page know how to inherit visual
02:02properties from the parent element that a theme is applied to.
02:05The way that you apply a theme is by using the data-theme attribute and then
02:10you give it a letter.
02:11So for example, data-theme="a", data- theme="b" and so on depending on the swatch
02:16that you want to use, and that will assign a theme to the element that you apply
02:22the data-theme attribute to, and if it's a parent element, that will affect the
02:26way that the children of that element apply as well.
02:29In fact, in some places, you can get even more advanced.
02:33List views for example go further than this.
02:35You can theme many of their parts individually and I've listed a table here
02:39that gives you idea of some of the different things you can do with list views and theming.
02:44So for example you can put themes directly on list items themselves.
02:49You can specify things like what the divider theme should be on a given list
02:53view, or how the count bubble should appear by using the data-count theme, you
02:57can specify things like what the icon should be in place of the standard
03:01right-pointing arrow in list views.
03:03Anyway, this gives you an idea of how powerful things are, but there's nothing
03:07like seeing it in action.
03:08So I am going to jump over to the code, and we can play around with themes and
03:10see how themes affect the visual appearance of the page.
03:14Now unlike in other examples, I don't have a start and finished state here.
03:18What I have is just an example theme_ tester file, and you can see that I've just
03:23put a whole bunch of content in here, different kind of page elements, and we're
03:26going to see how the various themes affect the way that this content is styled.
03:32So I am just going to go ahead and start assigning themes to various parts of
03:35the page and then we'll see what it looks like in the browser.
03:38So before I do anything, let me just run this in its current state, and you can
03:41see that this is the default jQuery Mobile theme,
03:45this is the out-of-the-box appearance if you don't do anything special.
03:48And you can see that the background color here is lighter and the text is
03:51darker, up here we have our header with an Options button in it, and here's the
03:56collapsible section control that we looked a little while ago.
03:59Here's a list view with some list items in it and a divider with some count
04:04bubbles, we've got a footer down here with some buttons.
04:06So this should be a pretty good indicator of how various parts of the page are
04:11going to look when we change the themes around.
04:13So let's go back to the code, and let's begin just by putting a theme right
04:17on the page itself.
04:18So right on the page I am going to say (data-theme="a") and I am going to save
04:24that, and I am going to go back to the browser and I am going to refresh it.
04:28Okay, so when I refresh the page, you can see that the difference was pretty dramatic.
04:33Now, the background is much darker, the text is whiter, the controls look a
04:37little bit different, you can see that the accordion control here, or the
04:41collapsible set looks a little bit different.
04:43But list view hasn't really changed all that much, but we'll get to that in a little bit.
04:46And you can see that the footer has changed as well.
04:49So let's go ahead and start playing around with different parts of the page.
04:52I can actually put a different theme right on the header.
04:55I can say (data-theme="d") and save that.
05:00All right let's refresh.
05:02So you can see that when I refresh the page, the rest of the page keeps theme A,
05:08but here, the header is now using theme D and you can see that the button and
05:12the header now look different from each other.
05:14So let's keep on going, let's do the same thing with the footer.
05:16Let's scroll down to the footer, (data -theme="d") and we'll save, and we'll
05:26refresh it, and you can see that now that my header and my footer look different
05:31than the rest of the page does.
05:33So let's keep on going, let's go back to the code.
05:35Let's put a theme right on the button that's in the header itself.
05:39So here's that Options button and I am going to put a theme, say (data-theme="e")
05:48and E is the fifth and last theme that jQuery Mobile ships with, but, you're
05:54going to find your own.
05:54So I will save and I will refresh and you can see now that the button is using
06:01theme E, whereas the header is using theme D and the page is still using theme
06:06A. So this should give you an idea of how customizable things are.
06:10Let's keep on going.
06:12Here in the collapsible set I will give this a separate theme as well, I will
06:15say data-theme="b", let's see what that does, we'll refresh.
06:22Now, you can see that the collapsible set is using theme B. Let's play with the
06:28listview a little bit.
06:29So down here in the listview, the first thing I am going to do is put a
06:34different style on one of the list items.
06:36So on the last list I will say )data -theme="e") and we'll refresh that.
06:43I am going to scroll down, you can see now that, that's using theme E, and theme
06:48e appears to have this like sort of yellow gradient applied to it, so you can
06:52see that's now affecting list item 6.
06:55Let's try some other stuff, let's go back to the code here, and right on the
06:59list view itself, let's put, let's see (data-divider-theme=)-- let's give that
07:08theme e as well and let's give the data- count-theme to affect the count bubble,
07:15let's give that a theme of b and last but not least, let's change the icon of
07:22one of these guys, let's change the icon on the second list item to the (data-icon="gear").
07:30So let's save, and now let's refresh, and let's keep an eye on the list view.
07:34So you can see that I change the divider theme here, now that matches theme E.
07:40The count bubbles are now using theme B which is this blue background with white text.
07:45Here, I change the icon from the standard right-pointing arrow to be the gear icon.
07:50So jQuery Mobile's theme architecture is pretty powerful and by using the themes
07:55on various parts of the page, you can get a really wide variety of visual
07:59appearances in your mobile pages.
08:01And using the new theme roller which just became available on their website, you
08:06can create your own themes, and download them and include them in your pages.
Collapse this transcript
7. jQuery Mobile Events
Touch events
00:00jQuery Mobile defines a whole set of events that your application can listen
00:06for and use to find out about interesting things that are going on in your
00:10mobile web application.
00:12And these events are divided up into four main categories;
00:15there are Touch Events, there are Scroll Events, Page Events and
00:20Orientation Events.
00:21Touch Events are used to simplify working with common touch gestures.
00:27Things that the user does throughout the course of using a mobile web
00:31application, like tapping on the screen or swiping from left to right or tapping
00:36their finger and holding it there, all those events are capturable by your app
00:40and you can trigger various things off of them.
00:44The Scrolling Events provide a cross platform way of binding two events that
00:49fire when scrolling starts and when it stops.
00:52The Page Events are also pretty useful.
00:54There are several events provided for handling page related issues such as when
00:59pages are created, when they're loaded, when they're about to be shown and when
01:03they are about to be hidden, when they're unloaded that kind of stuff.
01:07So you can hook into the page events to set up things for that page or figure
01:13out when a page is about to be unloaded and free some resources.
01:16There is a whole set of them and they're pretty useful.
01:19And then there is a Cross-platform Orientation event for handling when the
01:24orientation of the device changes.
01:26Now some devices like the iPhone, for example, support this event natively, but
01:31jQuery Mobile provides its own implementation for those devices that don't have
01:37a native way of handling when the device orientation is changed.
01:43So let's begin by taking a look at the Touch Events.
01:46Touch Events are pretty straightforward.
01:48There are five of them.
01:50There are the tap, taphold, swipe, swipeleft and swiperight events.
01:56And they are pretty self-explanatory.
01:58So for example tap is when the user taps on an element whether it's an element
02:03of the page or the page itself.
02:05A taphold is when the user taps on something and then holds their finger there
02:08for a second or longer.
02:11And then the swipe events are again pretty self-explanatory.
02:16Swipe events are typically triggered when the user's finger moves by more
02:21than 30 pixels in a given direction and less than 20 pixels in the
02:26perpendicular direction.
02:28So for example a swipeleft event is when the user moves their finger from the
02:32right towards the left by more than 30 pixels, but doesn't move their finger
02:36vertically by more than say 20 pixels.
02:39And you can bind to the swipe event and then figure out whether the swipe
02:43happened vertically or horizontally yourself or you can use the swipe left
02:47and swipe right events for the very common cases where you only care about horizontal swipes.
02:54So let's take a look at the code and see these in action.
02:58So here I am in the code and I've got my touchevents_start file opened and in
03:04my ExampleSnippets, I'm scrolled down to Chapter 7 and right here is my Touch Events code.
03:11So what I am going to do is right here in the head, I'm going to copy and paste
03:16my script for binding to some Touch Events.
03:20So I am going to copy these lines right here and I will paste those in.
03:27So notice that I am including this script after the jquery.mobile library
03:32itself and inside the script I am setting up my event handlers on my firstpage element.
03:41So I am using jQuery to get a reference to the firstpage object and then I am
03:45calling the live function.
03:47The reason I am calling live instead of bind is because remember that as jQuery
03:51Mobile is doing its work, it may be loading and unloading pages from the DOM and
03:56pages are being created and released.
03:59If I only use the bind event, then if that page ever goes out of focus or gets
04:05unloaded by jQuery Mobile, then any event handlers I had registered on it, are
04:09going to be discarded.
04:11So I am using live, so that whenever this page is created, the event handlers
04:17are created along with it and bounced to that page.
04:20And you can find out more about the live function either in the jQuery
04:25documentation or in the jQuery Essential Training title.
04:28So when the pageinit function gets fired, I am going to create three
04:34different event handlers.
04:35The first one is going to bind to the tap event.
04:38The second one is going to bind to the swiperight event.
04:42And then third one is going to bind to the swipeleft event.
04:46And in each case, I am just going to show an alert saying, hey,
04:49You either tapped or you swiped right or you swiped left.
04:52Again, nothing really all that earth- shattering in advance, but it just shows
04:56you how you can bind to these events.
04:58So let's go ahead and save this and just to show you what's going on down here
05:04in the markup, here's my first page, this is my jQuery Mobile page and it's got
05:10some content, it's got a header and a footer.
05:12So let's go ahead and run this in the browser and see what happens.
05:15So here is my page.
05:18Now I am going to just click the mouse somewhere in the content area.
05:23You can see that when I do that, I am getting a tap event.
05:25Now let's try swiping.
05:27So I am going to click the mouse and then move the mouse to the right.
05:32You can see I am getting a swiperight event along with a tap event because I did
05:38put my finger on the screen and same thing here, going to the right to the left,
05:42we are getting a swipeleft message along with a tap.
05:45So that's how easy it is to bind to swipe and tap events using the Touch Event
05:53module in jQuery Mobile.
05:55Now if you wanted to implement this yourself in JavaScript, using the various
05:59evidence that the different mobile browsers provide to you, you would have to
06:03write a whole bunch of custom platform specific code based on whether or not the
06:07browser is WebKit or whether it's something else.
06:10And jQuery Mobile just abstracts all of that away for you and just makes it
06:14really simple for binding to touch-based events.
Collapse this transcript
Orientation events
00:00To detect changes in the orientation of a device you can use the ORIENTATION EVENT
00:04provided by jQuery Mobile and that's called orientationchange and this is
00:09triggered when the orientation of the device changes.
00:12jQuery Mobile also sets a class name on the HTML element of either portrait or
00:17landscape when this happens.
00:19So if you have CSS classes that leverage these names then you can take advantage
00:24of that in CSS as well as a new script.
00:27So this only runs on the emulator.
00:30So what I'm going to do is just show you the code and now I'm going to jump over
00:33to the end of it to watch it running.
00:35So I'm going to switch over to the code and show you the code.
00:39So here I'm in the code and this is the section on orientation and I've got my
00:43orientevents_start open.
00:45So what I'm going to do here is copy this script and paste it into my file and save it.
00:54So what's happening is on the page in my document here I'm going to get
01:01reference that page using jQuery and then I'm going to attach to the pageinit
01:05event and then I'm going to bind to the orientationchange function.
01:10When the orientationchange function fires I'm going to say hey, the Orientation
01:15changed to and I'm going to say e.orientation.
01:20So this will be a string of either landscape or portrait showing me what the
01:24orientation changed to.
01:26Now this only works on the emulator.
01:28So at this point I'm going to save this and I'm going to jump over to the Mac
01:32and I'm going to show you how this works in the iOS simulator.
01:35So I'm over here on the Mac now and I'm running my iOS simulator and this is the iPhone.
01:41Now in order to test this I had to put my files into my local web server here on
01:46the Mac and I'm not going to go deeply into how to do that.
01:48If you watch my Mobile Web Fundamentals Essential Training course you'll see
01:53how to set that up or there are other titles here on lynda.com that cover the
01:57same kind of subjects.
01:58So basically what I'm doing is using the iPhone to load the orientation change
02:04starts file that we have right here.
02:06You can see its orientevents_start.html.
02:09And so what I'm going to do is just change the orientation of the device and
02:14we'll see an alert that show me that the orientation is changed.
02:18So I'm going to say Rotate Left and you can see that we're getting the
02:21Orientation changed to:
02:22landscape and if we change it back to Right you can see them getting
02:27Orientation changed to: portrait.
02:29So it seems be working correctly.
02:31So that's how you can use jQuery Mobile's orientation event to detect changes in
02:35the orientation of the device.
02:37And in response to orientation changes you can do all kinds of things.
02:40You can re-layout your page to be wider, you can apply different CSS classes,
02:44you can change the way that your pages appear based upon how the user is
02:48holding the device.
Collapse this transcript
Scroll events
00:00jQuery Mobile provides two events related to scrolling;
00:03there is the scrollstart event and scrollstop event.
00:07And as you might imagine the scrollstart is triggered when a user starts to
00:11scroll the content in the view and scrollstop is fired when the scrolling stops.
00:16Now there are a couple of things you need to be aware of when you're working
00:19with scrolling events on various devices.
00:22Perhaps the most important one is that currently iOS devices suspend DOM
00:27manipulation during scrolling.
00:29In other words, while the content has being scrolled in the window, you can't
00:33make changes to the DOM and that's done for performance and other reasons.
00:38What the iOS device does is it takes all of the DOM manipulation actions
00:43that your script might do and queues them up and executes them after
00:47scrolling has stopped.
00:49The other thing you need to keep in mind is that scrolling on mobile devices is
00:54a little bit different than it is on desktop devices.
00:57When the user scrolls on a mobile device, they typically flick with their finger
01:02or thumb and there is some inertial property associated with it, so the
01:06scrolling starts off pretty fast and then it starts to slow down a little bit
01:10and many users when they are reading long content, will just flick the content
01:14as it starts to slow down, but it never actually stops.
01:17And in those cases where the user is scrolling, but when the content starts to
01:22slow down up a little bit, they scroll again, you won't get a scrollstop
01:25message fired at that point, the scrollstop event only fires when the scrolling
01:30has come to a complete stop.
01:31So if you're counting on getting scrollstop events, you need to be careful about
01:35how you use these because in many cases the user might be scrolling in a way
01:39that won't fire the event in the way that you are expecting it.
01:43So let's just jump over the code and see the scroll event in action.
01:48So here I have my scrollevents_start file open and I'll go to my Snippets and
01:53I've scrolled down to my Scrolling section.
01:55I'll just copy this code right here and I will paste it into the head.
02:01So what's happening here is when the page gets initialized, I'm binding on the
02:06document here, not on the page, I'm binding on the actual document object to
02:11the scrollstop event.
02:13I am not using the scrollstart event here, just using scrollstop and I am just
02:17showing an alert that says that scrolling stopped whenever the scrolling action
02:21stops, just a simple example to show one of the events in action.
02:25So I'll save and I'll run this.
02:27And you'll see that when I grab the scrollbar and I start to scroll, when I stop
02:32scrolling, that event will be fired.
02:34So I am going to scroll and stop and you can see that I'm getting my scrolling
02:37stopped event and that's pretty much how it works when you scroll.
02:42You can see as I am scrolling, when it stops completely, that's when the event gets fired.
02:48So remember, just keep in mind that scrolling is a little bit different on
02:51mobile devices, there is the inertia involved, so the scrollstop event may not always fire.
02:57It only fires when the scrolling completely stops.
02:59And also just remember that on iOS devices, you can't change the DOM around
03:04while scrolling is happening.
Collapse this transcript
Page initialization and load events
00:00jQuery Mobile exposes a whole collection of page related events for your web
00:05application to use to figure out what's going on during the various lifecycle
00:09events of jQuery Mobile pages.
00:11They're divided into four categories.
00:14There is Page Initialization, Page Loading/ Unloading, Page Change, and Page Transition.
00:19Now each of these categories is named appropriately and implies what kind of
00:23events are contained within each category.
00:25So for example in page initialization these are events for handling the various
00:30page initialization related events, things like when pages are about to be
00:34created and when they're finished being created, and when they're initialized.
00:37Page Load/Unload again similarly, these allow scripts to handle page loading and
00:42unloading events and when page loading encounters some kind of a problem and a
00:47failure happens, now at this point you can take whatever action you want.
00:50You can navigate to a different page.
00:52You can show the user some kind of custom error message. It's up to you.
00:55The page change events are fired when pages are changed to and from using jQuery
01:01Mobile's programmatic method for changing pages.
01:05These are not fired when the user clicks in a link and a page is about to be
01:10changed to, because jQuery Mobile provides a programmatic function that you can
01:14call within your scripts to change pages for whatever reason that you deem fit.
01:19But similar to load/unload these events allow you to trap when a page is changed
01:25to and from and when a change event fails for some reason.
01:29Then finally there's the page transition events which as their name implies are
01:33fired before and after page transitions start and end.
01:37So a transition is a thing like a pop or a fade or a flip.
01:40You can trap when those things start and when they end.
01:44Let's start by taking a look at the page initialization and the page loading
01:48and unloading events.
01:49There is a sequence of events that fire when pages are initialized in jQuery Mobile.
01:55So if you take a look at a typical jQuery Mobile webpage that looks like this
01:59with its container element, in this case it's a section, but it could be a div
02:02or anything else with a data role of page when one of these is created there are
02:07three events that fire.
02:09The first is the pagebeforecreate event which is then followed by pagecreate and
02:13followed up by pageinit.
02:14Now each of these events has a specific context for why it's being fired.
02:20So for example page before create is triggered when the page is created, but
02:24before most of the widget auto initialization code has had a chance to run.
02:29So in this event it's your chance to manipulate any markup in the DOM before
02:33jQuery Mobile has a chance to do so.
02:36The page create event is triggered when the page has been created, but before
02:40the widget enhancement has been completed.
02:42So if you want to do any customization on widget enhancement, you can do it here
02:46and then finally there's the page init event which is fired when the page is
02:50initialized after jQuery Mobile has finished enhancing all the page content.
02:56And you use this event instead of the usual DOM ready event in jQuery.
03:00One of the first things you learn when you learn to use jQuery is there is this
03:05document ready event that fires when an HTML file has loaded and jQuery has had
03:10a chance to go through the DOM and do any initialization work that it does.
03:14You're taught to use that instead of the window on load eventHandler, because
03:18window on load fires only after all of the images have loaded whereas DOM ready
03:23fires when all the DOM content has been parsed and placed into the document.
03:28Now in the case of jQuery Mobile you have to remember there could be multiple
03:32jQuery Mobile pages inside a given HTML file.
03:36So you don't want to use DOM ready, because that's going to fire when the
03:39HTML file is loaded.
03:40But if you want to trigger your logic off of a specific page being initialized,
03:45you will use the pageinit event instead.
03:47Let's take a look at PAGE LOADING and UNLOADING.
03:50When an external page is loaded into the DOM there are two events that are going to fire.
03:55First is going to be the page beforeloadevent.
03:58This is fired before the page is loaded and then one of two things is going to happen.
04:03There is either going to be a page load event or there's going to be a page load failed event.
04:09So if you try to navigate to a page that doesn't exist for example or something
04:12goes wrong with the Ajax request, then you will get a page load failed.
04:16So page before load is triggered before jQuery Mobile formulates the Ajax
04:21request to load the external page, and you can trap this event if you want to do
04:25your own custom page loading logic here.
04:28The page load event is triggered when the page has been successfully loaded, has
04:32been parsed, and is inserted into the DOM by jQuery Mobile.
04:36The page load failed is then triggered by jQuery Mobile if for some reason the
04:40request fails and by default the framework shows the page load failed message,
04:44but you can whatever you want here.
04:46You can navigate to a different page or do some custom error messages.
04:49It's up to you and finally there's the page remove event and this is triggered
04:54when the framework removes a page from the DOM.
04:57So if the page has been navigated away from in jQuery Mobile needs to reclaim
05:01some memory, and it goes to remove the page from the DOM, you can trap that
05:06event and do any custom work that you need to do.
05:09Let's jump over to the code and see some of these in action.
05:13So here I am in the code and I've got my ExampleSnippet opened and I've got my
05:17pageload_start and pageinit _start example files open.
05:22So let's start with the page loading event.
05:24I am going to go to my snippets, copy over the script, and paste into the head.
05:34Now the pageload event is bound to on the parent document itself.
05:39So here I'm using jQuery to get a reference to the document and then calling the
05:43bind function to listen for the pageload event and then here's my eventHandler
05:48and my eventHandler takes two arguments.
05:49There is the event object and then there is some data that jQuery Mobile
05:53passes along and this is a pretty complex data object, but it's got some
05:56useful properties in it,
05:57for example, the URL of the page that's been loaded.
06:00You can use that information for whatever purposes you may have.
06:03In this case, I am just showing the alert that shows the URL of the page that got loaded.
06:08So let's save this and let's run it and what I am going to do is use the Web
06:15Inspector here in Safari.
06:16I am going to place a breakpoint on that line where we show the alert.
06:22So now I am going load the external page and you can see that we hit that
06:26breakpoint and I've got a watch expression on my data object and you can
06:31see that there is a whole bunch of information being passed in here by jQuery Mobile.
06:35Now some of this is pretty advanced, so I am not going to go deeply into it.
06:38But down here is the URL property and this is the URL of the page that's
06:42about to be loaded.
06:43So if we go ahead and let this run, you will see that my pageload event has been
06:48fired and it's showing the URL of the page being loaded.
06:51I click OK, the page is loaded, and everything is great.
06:55So let's go back to code, let's try something else, let's copy this, and let's
07:04bind to pageloadfailed and now I am going to refer to a page that doesn't
07:09exist, let's run that.
07:15And you can see that a page load failed event happened and you can see that's
07:19showing error page there.
07:20Let's just change this really quick just to say error.
07:26Let's run it again.
07:28And you can see that now my pageloadfailed event has been fired.
07:31There is an error and the default error message is shown.
07:38Now let's take a look at the page initialization events.
07:41So let's scroll over here to our page initialization events (Page Init).
07:44I am going to copy these, paste them in.
07:52Now in this case the pageinit, beforecreate and create events are going to be
07:57fired on the page itself.
07:59So I'm using jQuery selector here to get a reference to my first page object,
08:05which is this guy right here, and I'm creating eventHandlers for pageinit,
08:09pagebeforecreate, and pagecreate, and again I am just showing some alerts
08:15indicating that each one of these happened.
08:17So let's go ahead and save this and let's run it.
08:22So now pagebeforecreate has been fired.
08:24So the page is about to be created, but nothing has been initialized yet.
08:28Now pagecreate is fired and at this point the page has been created and the
08:32widget enhancement process is moving along, and then finally there's page init,
08:37in which case everything has been initialized, all the widgets have been
08:40enhanced, and everything is inserted into the DOM.
08:43So those are examples of using page initialization and loading and unloading
08:47events in jQuery Mobile.
Collapse this transcript
Page change and transition events
00:00Let's look at the Page Change and Page Transition events in jQuery Mobile.
00:04In jQuery Mobile there is a function to programmatically navigate pages and it's
00:09called the mobile.changePage function.
00:12Now we haven't covered this yet and we will in a little bit, but I wanted you
00:15to be aware of it because it's how you change from one page to another using
00:19script in jQuery Mobile.
00:21Now when this function is called, it causes two events to be fired.
00:25The first one is pagebeforechange event.
00:29Now the second event will be one of two things;
00:31they will either be a pagechange event, or a pagechangefailed event.
00:36pagechange will be fired if the change is successful and the new page can be
00:41changed too and the pagechangefailed event will be fired if for some reason
00:46something goes wrong, either the page being changed to has a parsing error or it
00:50can't be found or whatever it is, then that error will fire.
00:53And the pagebeforechange event is fired before any of that happens.
00:57It's fired before the change actually begins.
00:59So in the event of the pagechange event being successful, you can do whatever
01:04you need to do programmatically when a page is changed and if the
01:08pagechangefailed event happens then again you can take whatever action you
01:11need to take, either navigating to a different page or showing some error or whatever.
01:16Now for transitions, there are also events involved and page transitions involve two pages;
01:21there's a "from" page and a "to" page.
01:24Now if we imagine that this little black rectangle is the screen right here and
01:28the current page being shown in the screen is the fromPage, when a transition
01:32happens, the first thing that will happen is that the fromPage will get a
01:35pagebeforehide event and the toPage will get a pagebeforeshow event.
01:41Now at this point, both pages have been loaded, they have been parsed, they have
01:44been placed into the DOM by jQuery Mobile and they are both present.
01:48Now after those events are fired, the fromPage will animate out, the toPage will
01:52animate in and then the fromPage will get a pagehide event and then the toPage
01:57will get a pageshow event.
01:59So just to recap, we've got pagebeforeshow and beforehide and beforeshow is
02:03triggered on the page that's about to be transitioned to before the animation
02:08starts and similarly the pagebeforehide event is on the page that's about to be
02:12transitioned away from and again that's before the animation.
02:15And then the pageshow and pagehide events are triggered on their respective
02:20pages, again after the animation has completed.
02:24Let's take a look at this in the code.
02:26So here in the code, I have got my pagetrans_start file open and here is my Snippets.
02:31So let's go ahead and copy this over.
02:34So I'll copy this and paste it into the head.
02:37So let's take a quick look at what's going on here.
02:38Down here in the content I've got two pages;
02:41here is firstpage and here is secondpage and they're both being defined
02:45using HTML5 section tags.
02:48Now in this case they both happen to be in the same HTML file, but they could
02:53be in separate files.
02:55So let's go back up here.
02:56So what I am doing is I want to attach the pagebeforehide, beforeshow and then
03:02hide and show events to each one of the pages.
03:05Well, since those pages are being defined as section tags and the events are
03:10fired on the jQuery Mobile page construct, I'm using the jQuery selector
03:15mechanism here to get all the sections in the page, in this case, there are two
03:19of them, and attach event handlers for each one for beforehide, beforeshow and so on.
03:25Now, when these events happen, I am going to get a callback function which you
03:29see here which takes an event (evt) and take some associated data.
03:32Now the data object changes based upon which event is being fired.
03:36In the case of the pagebeforehide, this data object will be the next page, this
03:43data object will be the previous page and so on and we'll see that in just a
03:47moment, but for now, I am just going to save and run it and show you it running.
03:52So when Safari starts, you can see that there's a page being shown.
03:55Now there is no page being hidden because there's no current page and then
03:58after the page is currently shown, everything is fine and we only get those two
04:02events at that one time.
04:04But now watch when I click on Go to the second page, where you should get the
04:07alerts for the page being hidden, the page being shown and now there's the page
04:12hidden and the page shown alerts.
04:14So there were four alerts there;
04:16one for the page that's being hidden and then being shown and then after the
04:21page has been hidden and then after the page has been shown.
04:24So let's go back to the code and let's put a breakpoint on this line right here,
04:29so we can take a look at what's in the data object.
04:31So let's go back to the browser and what I am going to do is bring up the Web
04:36Inspector and going to go to the script for code right there, we'll put a
04:46breakpoint right there and you can see that when we reach the breakpoint, we
04:49should have a data object that we can look at.
04:52So I'm going to go ahead back into the browser and click on Go to first page.
04:57You can see we hit the breakpoint and now we have our alert that's about to be
05:02shown, but let's take a look at the Data object.
05:04So the data:
05:05Object in the case of the pagebeforehide has a nextPage object in it and this
05:10gives you all the information about the page that's about to be shown.
05:15So this is called on the page that's being hidden and you're given information
05:19about the page that's coming up.
05:21So if we look inside the HTMLElement object here, this provides you all the
05:26information about the page that's about to be brought in.
05:29So let me go ahead and put another breakpoint there and we are going to run this
05:34and you can see the page is being hidden and the page is being shown.
05:37Now the pagehide alert is about to be triggered.
05:40And what we are going to do is go back up here and we're going to take a look at
05:44our object and once again we have a nextPage.
05:46Let's have another breakpoint right there because this is another nextPage object.
05:51Let's run until we get to the pageshow.
05:54And now in the pageshow case, we have a prevPage object and this is the data:
06:00Object for the page that was being shown and again you can see there is all of
06:04this information here for you to make use of, lots of data being provided to
06:08you by jQuery Mobile.
06:09So that's a quick summary of using the page transition events in jQuery
06:13Mobile and trapping events to see what's going on in the page transition
06:18event handlers.
Collapse this transcript
8. The jQuery Mobile API
Configuring defaults
00:00In addition to providing a whole bunch of really great features for marking up
00:04and working with mobile webpages, jQuery Mobile actually provides a full API for
00:10working with the library itself and it does that by exposing a mobileinit event
00:16that you can use to initialize your application's custom settings.
00:20This is your chance when your application starts up to make any customizations
00:25you want to the way that jQuery Mobile does things.
00:28Now the mobileinit event fires immediately when the framework loads.
00:34So in order to take advantage of this event, you have to sandwich any
00:38customization code that you have in between the loading of jQuery and the
00:43loading of jQuery Mobile.
00:45So typically, you have your script loader that looks like this;
00:48you have a script tag that refers to the jQuery library and then you have
00:52another script tag that refers to the jquery-mobile library.
00:55And in order to make sure that your code gets executed before jQuery Mobile just
01:00goes off running and doing its thing, what you got to do is put your script in
01:04the middle here and what I am doing here is binding on the document to the
01:09mobileinit event and calling an initialization function that I'll define either
01:15here or somewhere else.
01:16But the main point here is that you have to have this script in between jQuery
01:20and jQuery Mobile, that way your script can do any customizations on the jQuery
01:26Mobile library before it goes off and running.
01:28So then that begs a question well, what would you like to customize?
01:32Well, jQuery Mobile provides a whole bunch of settings all accessible via the
01:37$.mobile object and so for example you'd have $mobile.
01:41and then the name of some setting and you can set that setting to some newValue.
01:46Now there's a whole bunch of settings that you can customize in jQuery Mobile,
01:50but I have provided here a table that lists the most common ones that you are
01:55probably going to want to customize.
01:58So for example, the activePageClass is the CSS class that's applied to the
02:03currently active page and during transitions.
02:06Now it defaults to a class called ui- page-active, but you can change that to
02:10whatever you want here.
02:11So if you come up with some CSS class that you like to apply to whatever the
02:16active page is, you can customize that when your app starts up.
02:19The ajaxEnabled setting determines whether pages are loaded via AJAX requests.
02:25Now this defaults to true because one of the major things that jQuery Mobile
02:29does, it makes AJAX requests for external pages, loads them asynchronously,
02:34parses them and inserts them into the DOM with these nice animations.
02:38Now you can turn that behavior off if you want, but it defaults to being on.
02:42The next two settings defaultPageTransition and defaultDialogTransition are the
02:47default transitions used for pages.
02:49Now throughout this title, you've probably noticed that the transitions
02:53you've been seeing when I change from one page to another, has been the slide
02:57transition and that's because the default value for the defaultPageTransition
03:01setting is a slide and similarly for dialogs, the defaultDialogTransition is pop.
03:07But you can change them to be whatever you want.
03:09You can use fade or flip or pop or slide whatever you want to use.
03:13The loadingMessage and pageLoadErrorMessage settings are the messages shown when
03:19a page is loading and when a page can't be loaded.
03:22And in the first case, it defaults to the word loading and in the second case,
03:25it defaults to error loading page, but you can change that to be whatever you want.
03:29So let's take a look at some of the customizations in action.
03:33I am going to fire up my Code Editor over here and you can see I have got my
03:37Snippets opened and I've scrolled down to the Config section and I have got my
03:41config_start file open over here.
03:43So what I am going to do is copy this script right here and then I'll explain
03:50it in a moment, but now I have to insert it in a very specific place and
03:53remember I have to insert it right here between the jquery library and the
03:58jquery.mobile library.
04:00So I am going to select that line and paste and so now I've got my script
04:04sandwiched in between the two and on the document object itself I am going
04:09to listen for the mobileinit event and when that event fires, I am going to
04:14call this function right here, which will go and customize my settings in jQuery Mobile.
04:19First, it's going to show an alert that says the Mobile initialization event
04:22fired and then we are just going to go ahead and change some settings.
04:26We'll change the defaultPageTransition to be a fade, we'll make the
04:29defaultDialogTransition be a flip and we'll change the
04:33pageLoadingErrorMessage to be Whoops!
04:36instead of error loading page.
04:38So let's go ahead and save and then down here you can see I have got some
04:42exercise code that's going to work some of this.
04:45So I've got a link that goes to a secondpage, I've got a link that tries to load
04:50an externalpage that's nonexistent, let's actually make sure that's nonexistent
04:55and then we will just exercise this file.
04:58So let's go ahead scroll back up to the top and we'll click Run and you can
05:03see okay, there is the Mobile initialization event, it has now fired and
05:07here's my first page.
05:08Now if you look at the code you will see that my defaultPageTransition is a fade
05:13operation right here.
05:14So when I go to the second page, instead of getting a slide effect, I should get
05:18a fade which is what happens and when I go back to the first page it should fade
05:22back, that's very good. All right.
05:24So now let's try to load the external page which doesn't exist.
05:27Now ordinarily, I would get an error that says error loading page, but
05:31let's watch what happens.
05:32So I try to load an external page, and it says Whoops!
05:35That's because I customized that. All right.
05:37Let's go back to the code.
05:39Let's try loading the secondpage as a dialogue.
05:44Remember to do that from earlier.
05:45We have to do data-rel="dialog", to do that.
05:49So I am going to save and we are going to go back and run this again.
05:53Okay there is my event.
05:54So now when I go to the second page, I should get a flip.
05:57So let's look at the code. Yeah.
05:59So now I have my defaultDialogTransition is a flip, normally it's a pop.
06:04So let's go back to the browser.
06:06So when I go to the second page, instead of seeing a pop, I should see a flip
06:09and that's exactly what happens.
06:11So that should give you an idea of how customizable jQuery Mobile is.
06:15I've just shown you a couple of simple examples, but you can customize a whole
06:18bunch of things in jQuery Mobile from the CSS classes that are applied to pages
06:22to all kinds of things.
Collapse this transcript
Utility methods
00:00jQuery Mobile also provides a set of utility methods and properties that you can
00:05make use of in your scripts, should you so choose and in this table, I've listed
00:09some of the more common ones.
00:11There is the changePage method which programmatically changes the current
00:16page to another page.
00:18So instead of just waiting for the user to click on something, you can
00:21change pages yourself.
00:23There is also the silentScroll method and that scrolls to a given Y position on
00:28the page without activating any of the scroll event listeners.
00:32This is useful because in some cases where you want to have a link down to a
00:36named anchor in the page remember because jQuery Mobile is using that hash
00:41character (#) to do things like load other pages that are in the same HTML file,
00:45you can't use named anchors in your HTML pages.
00:49So if you want to scroll down to another element in the page, you need to use
00:54silentScroll and it'll do that without activating any of the scrolling event
00:58listeners that might be attached to the page itself.
01:01There is the activePage property and that represents the currently active page
01:05object and then there are the methods for showing and hiding the PageLoadingMsg.
01:11So if for some reason you decide to override the AJAX functionality that loads
01:16pages into jQuery Mobile with your own, then you can use these methods to show
01:21and hide the page loading dialog.
01:23So let's take here a quick look at changing pages via script.
01:26So to change page via script you use the changePage function and that takes two arguments.
01:32There is the 'to' argument and the 'options' argument.
01:35Now the 'to' argument is the page to change to.
01:39It's either a URL in which case you give it an absolute or a relative URL to
01:44change to in the case of a string or in the case of an object, you've used
01:49jQuery to get a reference to some other page elements in the HTML file and you
01:55want a change to that. Now that's enough,
01:57the options argument is actually optional.
01:59You don't have to pass it if you don't want to, but if you do, there are a few
02:02things that you can specify.
02:04Now in the options object, you can specify AllowSamePageTransition which allows
02:09transitions to the same page, so if you are going from and to the same page, you
02:15can specify that the transition should apply there.
02:18Now that defaults to false, but you can set it to true if you want to.
02:22There's also the reloadPage option which forces a page reload, even if it's
02:27already in the DOM and again that defaults to false, but you can force pages to
02:32be reparsed or reloaded, even if they're already present.
02:35There's also the showLoadMsg and that's whether or not to show the loading
02:39message, that defaults to true, but you can turn it off if you want to and then
02:43there's the transition and that's the transition to use when changing to the new page.
02:47If you don't specify it, it uses whatever the default transition is or you
02:51can list it yourself.
02:53Couple of other utility functions that are pretty useful, the ones that I've
02:56chosen here to list are usually working with URLs because you find working with
03:01URLs is A. Usually a pain and B. Sometimes pretty common.
03:06So jQuery provides some utility functions for working with URLs.
03:09There are a couple of functions to determine whether or not a URL is relative or is absolute.
03:15There's a function called isSameDomain which will compare two URLs to see if
03:19they're in the same domain, including the protocol.
03:21So for example if you'd have http://joe.com and https://joe.com, those are not the same.
03:29The protocols have to be the same.
03:31And then there is parseUrl which is a method that will take a URL and parse it
03:35into pieces that can be really easily accessed, such as the domain, the
03:40protocol, the query string, the path, the relative path, the filename, the
03:45filename extension, all that good stuff.
03:48So let's just take a quick look at one of the utility functions that's changing
03:53from one page to another programmatically because this is something you are
03:55probably going to run into fairly commonly.
03:59So here I am in the code with the utility snippets scroll to and here is my utility_start.
04:05So what we are going to do is build a little function that changes
04:09pages programmatically.
04:11So I am going to copy this script right here and that contains my change function.
04:15And I am going to paste that in here.
04:17I am going to go back to my Snippets.
04:20And then I've got two buttons.
04:22I am going to copy this one and put that into my content section, right here
04:31and I'll copy the other one and we'll put that into my section down here, on the secondpage.
04:40So now I've got two buttons and when the buttons get clicked they call this
04:44doChange function and the doChange function passes in the ID of the page we want to change to.
04:52You can see that the title of the button is Go to Page whatever Via Script.
04:56This is not a link that's happening here, this is a programmatic change.
05:00So back up in the doChange function, I've got the newPage I am going to and I
05:05am going to call $.mobile.changePage and that will take me to the newPage, so
05:09let's save and run.
05:12So I am going to click on Go to Page 2 Via Script.
05:14You can see that that works and Go to Page 1 Via Script.
05:18It seems to be working just fine.
05:20So we're using the changePage function to programmatically change pages.
05:24Let's go back and make a slight edit.
05:26Let's comment this one out and uncomment this one because here I'm specifying a
05:31transition for sliding up rather than using whatever the default is and this is
05:37using the options object.
05:39Remember here I didn't pass it in because it's optional, but here I am going to
05:42use the transition feature.
05:43So I'll save and we'll run it.
05:46So now when I click on this button, I should get a slide up effect, and I do
05:51and that should show you a pretty good example of using some of the jQuery
05:56Mobile utility functions.
Collapse this transcript
Conclusion
Goodbye
00:00All right. Well that wraps up our exploration of jQuery Mobile.
00:04I hope you've enjoyed joining me here as we see how jQuery Mobile can help you
00:08build really visually rich, almost native looking mobile websites.
00:12Where you might want to go next is if you're not familiar with the concepts and
00:17fundamentals of building mobile web applications, there is a great title here
00:22on the lynda.com that covers the fundamentals of mobile website development and design.
00:27So even though jQuery Mobile is pretty good for building mobile websites and
00:32mobile web applications there's additional information that's applicable to
00:36mobile web development in general.
00:38So I would go, check that out next.
00:40I'd also go, check out the jQuery Mobile website and see some of the examples on
00:45there because that website and jQuery Mobile are being updated all the time.
00:50So there are new features being added that I did not get a chance to cover here,
00:54go check out the jQuery Mobile gallery as well for some inspiration on some ways
00:59that you can build mobile web applications with the jQuery Mobile library.
01:03Well, I am really excited to see what all of you build with jQuery Mobile,
01:06thanks, good luck and happy coding.
Collapse this transcript


Suggested courses to watch next:

JavaScript Essential Training (5h 31m)
Simon Allardice

jQuery Essential Training (4h 52m)
Joe Marini


HTML5: Structure, Syntax, and Semantics (4h 34m)
James Williamson


Are you sure you want to delete this bookmark?

cancel

Bookmark this Tutorial

Name

Description

{0} characters left

Tags

Separate tags with a space. Use quotes around multi-word tags. Suggested Tags:
loading
cancel

bookmark this course

{0} characters left Separate tags with a space. Use quotes around multi-word tags. Suggested Tags:
loading

Error:

go to playlists »

Create new playlist

name:
description:
save cancel

You must be a lynda.com member to watch this video.

Every course in the lynda.com library contains free videos that let you assess the quality of our tutorials before you subscribe—just click on the blue links to watch them. Become a member to access all 98,718 instructional videos.

start free trial learn more

If you are already an active lynda.com member, please log in to access the lynda.com library.

Get access to all lynda.com videos

You are currently signed into your admin account, which doesn't let you view lynda.com videos. For full access to the lynda.com library, log in through iplogin.lynda.com, or sign in through your organization's portal. You may also request a user account by calling 1 1 (888) 335-9632 or emailing us at cs@lynda.com.

Get access to all lynda.com videos

You are currently signed into your admin account, which doesn't let you view lynda.com videos. For full access to the lynda.com library, log in through iplogin.lynda.com, or sign in through your organization's portal. You may also request a user account by calling 1 1 (888) 335-9632 or emailing us at cs@lynda.com.

Access to lynda.com videos

Your organization has a limited access membership to the lynda.com library that allows access to only a specific, limited selection of courses.

You don't have access to this video.

You're logged in as an account administrator, but your membership is not active.

Contact a Training Solutions Advisor at 1 (888) 335-9632.

How to access this video.

If this course is one of your five classes, then your class currently isn't in session.

If you want to watch this video and it is not part of your class, upgrade your membership for unlimited access to the full library of 1,899 courses anytime, anywhere.

learn more upgrade

You can always watch the free content included in every course.

Questions? Call Customer Service at 1 1 (888) 335-9632 or email cs@lynda.com.

You don't have access to this video.

You're logged in as an account administrator, but your membership is no longer active. You can still access reports and account information.

To reactivate your account, contact a Training Solutions Advisor at 1 1 (888) 335-9632.

Need help accessing this video?

You can't access this video from your master administrator account.

Call Customer Service at 1 1 (888) 335-9632 or email cs@lynda.com for help accessing this video.


site feedback

Thanks for signing up.

We’ll send you a confirmation email shortly.


By signing up, you’ll receive about four emails per month, including

We’ll only use your email address to send you these mailings.

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

By signing up, you’ll receive about four emails per month, including

We’ll only use your email address to send you these mailings.

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked