• Tidak ada hasil yang ditemukan

jQuery-Mobile.pdf

N/A
N/A
Nguyễn Gia Hào

Academic year: 2023

Membagikan "jQuery-Mobile.pdf"

Copied!
130
0
0

Teks penuh

The Nutshell Handbook, the Nutshell Handbook logo, and the O'Reilly logo are registered trademarks of O'Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks.

Preface

Introduction

What This Book Covers

What You Need To Know

Conventions Used In This Book

Using Code Examples

Preface | xi

Safari® Books Online

How to Contact Us

Acknowledgments

Preface | xiii

Meet jQuery Mobile

Overview of the jQuery Mobile Library

How jQuery Mobile Works

Create Your First jQuery Mobile Application

In the next chapter, we'll discuss the details of pages and views in jQuery Mobile. We'll learn more about it in later chapters, so for now we'll just focus on the data role attribute.

Create Your First jQuery Mobile Application | 3

Or we can mark up the same content using the HTML 5 section, header, and footer tags, as shown in Example 1-3. By applying the appropriate data role attributes, our HTML 5 markup will be written as shown in Example 1-4.

Figure 1-1. First page in Safari
Figure 1-1. First page in Safari

Create Your First jQuery Mobile Application | 5

Navigating between pages is easy: just add a link to your content area on the first page of the application, as shown in Example 1-6. Tapping the back button will return you to the previous page and again jQuery Mobile will automatically handle the page transition.

Figure 1-3. Link to next page
Figure 1-3. Link to next page

Under The Hood: the jqmData() Custom Selector

Create Your First jQuery Mobile Application | 7

Application Structure and Navigation

Pages

Internal Pages

Pages | 11

External Pages

This will produce the screen shown in Figure 2-4 on the third page of our application.

Pages | 13

And when we tap the "Go to external page" link, jQuery Mobile will display the loading dialog and try to fetch and insert the external.html page. And it now becomes part of the application and can be accessed from any other page as if it were included in the original DOM.

Overriding Asynchronous Page Fetching

When creating pages that will be loaded asynchronously, make sure you don't introduce duplicate IDs into the original DOM.

Under The Hood: Page Initialization in jQuery Mobile

Triggers the beforecreate event (see “Initialization Events” on page 83 in Chap- ter 5 for more information)

Adds the ui-page class to all page elements

Looks for child elements that have a data- attribute and

Then it enhances form controls, buttons, and control groups (see Chapter 3 for more information on these individual elements)

Finally, it fixes toolbars as specified (see “Positioning the Header and Footer” on page 43 in Chapter 3 for details)

Pages | 15

Other more complex interface elements (like list views) are more refined, and jQuery Mobile will even change the markup, usually by adding span or div tags. Unfortunately, most browsers limit their feed view capabilities to only displaying tags that have been downloaded from the server, without any modifications that JavaScript might make afterwards.

Page Hide and Show Events

Pages | 17

For example, if you bind a click event listener to an element inside the page view event, that click event listener will be bound every time that page is displayed. If you only use this page once, this is fine, but if the user visits this page multiple times, the event listener will be bound multiple times.

Under The Hood: A jQuery Mobile Page Initialization Pattern

Upon document ready, initialize anything that needs to happen on the first page, plus any event listeners for elements throughout the application

Bind pageshow and pagehide events to pages as needed to handle transitions to and from them

Pages | 19

Dialogs

Navigation and History

Navigation and History | 21

Go to #page2 when .back button is clicked, show animation in reverse order, // and don't update the location hash.

Transitions

As shown in Example 2-10, you can reverse any transition by specifying the data-direction="reverse" property, although this is more useful for some transitions (such as slide and flip) than others (such as fade and pop). .

Under The Hood: Animations in a jQuery Mobile Application

Transitions | 23

Transitions are defined as rules within the jQuery Mobile style sheet, and you can use them directly if you want to animate elements in your app beyond the page transitions that jQuery Mobile provides.

Page Elements

Under The Hood: jQuery Plug-ins and Widgets

List Views

Basic List View

List View Buttons

List Views | 27

List View Dividers

Advanced List Views

Nested Lists

List Views | 29

List Views | 31

List View Split Buttons

Thumbnails and Icons

List Views | 33

As of this writing, jQuery Mobile seems to expect icons and thumbnails to be contained within anchor tags within list items, even if the list item is not intended to be touchable. Since jQuery Mobile will ignore any links with "#", it is not a problem to include the anchor tag even when it is not semantically valid.

List Views | 35

Count Bubbles

As of this writing, enumeration bubbles only work inside anchor tags inside list items, or inside list view separators (list items with data-role = "separator" applied to them).

List Views | 37

Under The Hood: Updating a List View

Toolbars

Navigation Bars

Toolbars | 39

Toolbars | 41

Positioning the Header and Footer

Toolbars | 43

Buttons

As of this writing, the jQuery Mobile documentation says that by applying the data-inline="true" attribute to a containing element, all buttons contained therein will be rendered inline.

Buttons | 45

Button Control Groups

Button Icons

Buttons | 47

Custom Icons

Accessing Form Elements with JavaScript

Checkboxes and Radio Buttons

Flip Toggle

Manually flip the slider and update its UI state. rotating itself will not automatically update the UI) mySlider[0].selectedIndex = 1;.

Input Fields and Textareas

Manually flip a slider and update the UI status. just flipping it won't update the UI automatically) mySlider[0].selectedIndex = 1;. document).ready(function() { // Disable an entry.

Search Fields

Select Menus

Custom Styled Select Menus

Disabled Elements

Multiple Selections

Optgroups

Placeholders

Sliders

The user can either slide the slider back and forth, or enter a value in the input field and the slider will update automatically.

Figure 3-25. Slider element
Figure 3-25. Slider element

Layout Grids

Layout Grids | 61

Layout Grids | 63

Theming jQuery Mobile

Themes and Swatches

Themes and Swatches | 67

Themes and Swatches | 69

Themes and Swatches | 71

Themes and Swatches | 73

Under The Hood: Customizing a Swatch

Themes and Swatches | 75

If we load our new style sheet into a page set to use Swatch E (Example 4-4), we can see the results (as illustrated in Figure 4-7). Note that I've included my custom Swatch E style sheet after the jQuery Mobile style sheet, thus ensuring that my styles will override the default styles.

Themes and Swatches | 77

Theming List View Elements

Events

Touch Events

Under The Hood: Using Swipe Events to Trigger Page Transitions

Events | 81

We want our new app to respond to swipe events: swiping left should cause an animated transition to the next page, and swiping right should cause an animated transition to the previous page. The result is an application that responds to sweep events in any direction and transitions to the appropriate page.

Initialization Events

Events | 83

As jQuery Mobile hides and shows pages, it triggers before and after events on both the page being hidden and the page being displayed. This event fires on the page being transitioned before the transition between pages begins. This event fires on the page being transferred to before the transition between pages begins.

Scroll Events

Orientation Change Events

Responsive Layout API

CSS Selectors

Responsive Layout API | 85

For example, let's say you want to change the background of the app depending on the orientation of the device. To achieve this, you only need to write a few CSS rules that fall from the .portrait and .landscape classes (Example 5-7). In this example, each section marker will have either a background image (if the device is in portrait mode) or a green gradient (if the device is in landscape mode).

Screen Size Breakpoint Classes

The addResolutionBreakpoints method accepts an integer (corresponding to a resolution breakpoint) or an array of integers (each corresponding to a resolution breakpoint) as an argument, and jQuery Mobile will automatically apply the appropriate classes to the HTML element. In addition to automatically applying CSS classes to the HTML markup, jQuery Mobile also fires orientationchange events whenever the user changes the orientation of the device. You can change your layouts programmatically or even load different content for different orientations (Example 5-10).

Adding Size Breakpoints

Here you will see one image if your screen resolution is between 320 and 480 pixels wide, another image if it is between 480 and 768 pixels wide, and a green gradient if the screen size is larger. You can add your own using the addResolutionBreakpoints method, as I did in Example 5-9. By using this event, you can create even more dynamic layouts than you could with just CSS selectors alone.

Responsive Layout API | 87

Media Queries

Configuring jQuery Mobile

Available Options

Enable or disable asynchronous features: ajax form handling, ajax link handling and URL hash management. To use this attribute, define a function that retrieves the attribute in question and returns either true or false (see Example 5-13). If a page has a datarel or dataroll set to one of these selectors, it will not be included in the URL history hash.

Changing an Option via mobileinit

Configuring jQuery Mobile | 89

As jQuery Mobile starts its execution, it fires the mobileinit event and you can bind to it like any other event. However, mobileinit happens as jQuery Mobile is loaded, so if you want to bind to it, you'll need to load your event handler script before loading jQuery Mobile (Example 5-12). Here we include a new script file, jquery-mobile-defaults.js, which will run before jQuery Mobile and contains the mobileinit event handler shown in Example 5-13.

Under The Hood: Namespacing Data Attributes

In this example, we set the default page transition to "flip" and the loading message to "Please wait.." In addition, we detect whether the browser supports CSS transitions or not and require it via gradA. Through the $.mobile.ns configuration option, you can easily set jQuery Mobile to handle namespaces (Example 5-14). You can still easily select your name-spaced data attributes using the jqmData() selector, which automatically takes into account the namespace you define in the $.mobile.ns property.

Configuring jQuery Mobile | 91

Application Pages

In the header, a button links to the settings page, and we have defined the appearance of the button specified in the transition between the main page and the settings page. Here we have an input field for the Twitter username and a slider to change the desired number of tweets. The basic views that we will use to display the information from the Twitter API are shown in Figures 6-1, 6-2, and 6-3.

Application Pages | 95

Initializing the Application

The initMainPage Method

Initializing the Application | 97

Under The Hood: Passing Data Between jQuery Mobile Pages

DOM storage

Accessing the Twitter API

It has two methods: JSON.parse() (which takes a string in JSON format and returns an object) and JSON.stringify() (which takes an object and returns a string in JSON format). If you are not familiar with JSON, you can read about it at http://www.json.org/ where there are many resources, tutorials and examples. When the request is made and the service responds, the error function will be fired (if something went wrong) or the success function will be fired.

Fetching the Data

JSON stands for "JavaScript Object Notation" and is a specification for representing JavaScript objects as formatted strings. If you've never used this, you can read more about it in the documentation, but basically it provides a cross-browser AJAX implementation. The object will consist of an array called "results" and each item in the array will be an object that will look something like this:.

Initializing the Application | 99

Pass the tweetJSON object to the detail page so that it // has the information it needs. This function sends a GET request to the Twitter API, processes the response, and creates a list view. It also sets up a click listener for each list item so that when the user taps an item to go to the detail page, the data on the detail page is updated with the information to be displayed.

Initializing the Application | 101

Every time we show this page, we need to check if we need to update.

The initSettings Method

The initDetailPage Method

Error Dialog

Initializing the Application | 103

Improving the Interface

CSS Tweaks

Interaction Tweaks

A big improvement would be to let the user know something is happening, and jQuery Mobile's load dialog is the perfect tool. You can manually invoke the loading dialog using $.mobile.pageloading() and then hide it again using $.mobile.pageloading(true).

Improving the Interface | 107

This gives the user visual feedback that the application is doing something, instead of just being arbitrarily frozen. Finally, if the user enters an invalid Twitter username on the settings page, the Twitter API will not return any results, and right now it just results in a blank list view page. We should display a dialog message to let the user know that they entered an invalid Twitter username.

Improving the Interface | 109

If the user enters a new Twitter username to search for, the app displays a loading dialog (Figure 6-8). And if the user enters an invalid Twitter username to search for, the app will display the error popup Figure 6-9. There are plenty of other improvements to jqmTweet: Allowing the user to create tweets and handling conversations are the first two features that come to mind.

Figure 6-8. jqmTweet: Loading dialog
Figure 6-8. jqmTweet: Loading dialog

Overall Approach

Iterate: improve the results, add features, and fix bugs

Overall Approach | 111

About the Author

Colophon

Gambar

Figure 1-1. First page in Safari
Figure 1-2. First page in iPhone
Figure 2-1. Multiple pages (page 1)
Figure 2-2. Multiple pages (page 2)
+7

Referensi

Dokumen terkait

Figure 9 is a test display on the location type master data page on the data insert function when the location type data is filled with data that already exists in the