JQuery JQueryMobile: Difference between revisions

From Wiki RB4
 
(39 intermediate revisions by 3 users not shown)
Line 14: Line 14:
   $(this).hide();
   $(this).hide();
  });
  });
===Libraries===
In order to use JQuery and JQuery Mobile include:
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="resources/css/jquery.mobile-x.x.x.min.css" />
<script src="https://code.jquery.com/jquery-x.x.x.min.js" type="text/javascript"></script>
// event handler for mobileinit event
<script src="resources/js/libs/jquery.mobile-x.x.x.js" type="text/javascript"></script>


===Mobile Pages===
===Mobile Pages===
'''jQuery Mobile''' is a touch-optimized web framework for creating mobile web applications. jQuery Mobile is built on top of the jQuery library. jMobile web pages consist of different [[#page_role|pages]] regularly as part of a single HTML file and each with a header, content and footer section:
'''jQuery Mobile''' is a touch-optimized web framework for creating mobile web applications. jQuery Mobile is built on top of the jQuery library. jMobile web pages consist of different pages (data-role="page") regularly as part of a single HTML file and each with a header, content and footer section:
  &lt;div data-role="page" id="p1">
  &lt;div data-role="page" id="p1" data-title="Title">
   &lt;div data-role="header" id="p1">...</div>  
   &lt;div data-role="header" id="p1">...</div>  
   &lt;div data-role="content" id="p1">...</div>  
   &lt;div data-role="content" id="p1">...</div>  
   &lt;div data-role="footer" id="p1">...</div>  
   &lt;div data-role="footer" id="p1">...</div>  
  &lt;/div>  
  &lt;/div>  
  &lt;div data-role="page" id="p2">
  &lt;div data-role="page" id="p2">  
   ...
   ...
  &lt;/div>
  &lt;/div>
Transition is often implementet as  
 
  <a href="p2">...</a>
====Page Transition====
When the HTML file is loaded, the first page in the body of the file is displayed. If the application is split to multiple html pages, first page is loaded normally. Its HEAD and BODY is loaded into the DOM. That content will stay there (unless page is refreshed) to await further content loading. When second page is loaded, only its BODY content is loaded into the DOM, and when I say its BODY content I mean DIV with an attribute data-role=”page” and its inner content.
Transition to another page is often implementet as  
// html
  <a href="'''#'''<PageID>">...</a> // page inside the same html
<a href="xyz.html">...</a> // new html page loaded via Ajax
<a href="" data-rel="back">...</a> // back button
   
   
This may not sound as something problematic but you should think twice. What if we have several HTML pages and every and each page has something unique, lets say different javascript intended to be used only during that page execution, not to mention additional css files. Everything found in a HEAD of those files are going to be discarded and its javascript is not going to be executed. In order to switch to a complete different html file from a anchor use the '''rel''' attribute.
// javascript
$.mobile.changePage(<PageID>);
window.location.href = "mobile.html";
When the HTML file is loaded, the first page in the body of the file is displayed. If the application is split to multiple html pages, first page is loaded normally.
 
====Passing Data between Pages====
There are different methods:
* because everything is loaded during the HTML file load one option is a global data object in the global namespace
* to get data from the previous page you can register a function for the <code>pagebeforechange</code> event, the data parameter has a <code>prevPage</code> and a <code>toPage</code> member


===Templates===
===Templates===
Line 36: Line 56:


===Events===
===Events===
In JQuery (not in JQM, see pageinit event below) when the DOM is loaded completely the document.ready() function is called therefore any initialization should go here:
$(document).ready(function() {
  ...
});
As of version 1.7 bind an event to a function by the on() function , which replaces all old binding types:
$( "#members li a" ).on( "click", function( e ) {} ); 
Obsolete:
$(document).bind("mobileinit", function () { Notes.controller.init(); });
$("p").click(function() { ... });
$("p").hover(function() { ... });
$( "#members li a" ).live( "click", function( e ) {} );
$( "#members" ).delegate( "li a", "click", function( e ) {} );


====JQuery Mobile Events====
====JQuery Mobile Events====
jQuery Mobile offers several custom events that build upon native [http://api.jquerymobile.com/category/events/ events] to create useful hooks for development:
jQuery Mobile offers several custom events that build upon native [http://api.jquerymobile.com/category/events/ events] to create useful hooks for development. The sequence can be found [http://bradbroulik.blogspot.com.br/2011/12/jquery-mobile-events-diagram.html here] and a concept description [http://www.gajotres.net/document-onpageinit-vs-document-ready/ here]:
*mobileinit
*mobileinit // any event handler <code>($(document.bind("mobileinit", ...)</code>hast to be defined before JQM include because it is fired immediately
*pagechange
*pagechange
*pageinit: in jQuery Mobile, Ajax is used to load the content of each page into the DOM as you navigate.Because of this, $(document).ready() will/may trigger before your first page is loaded and every code intended for a page manipulation will execute only after a page refresh
*swipe
*swipeleft
*swiperight
*tap
*taphold


The sequence can be found [http://bradbroulik.blogspot.com.br/2011/12/jquery-mobile-events-diagram.html here].
===Icons===
 
* see http://demos.jquerymobile.com/1.4.5/icons/
To bind them to a function use:
$(document).bind("mobileinit", function () { Notes.controller.init(); });
 
====Standard Events====
To assign a click event to all paragraphs on a page and to define behavior:
$("p").click(function() { ... });


===data roles===
===data roles===
Line 57: Line 91:


====content role====
====content role====
Obviously just convention without impact.


====controlgroup role====
====controlgroup role====
Line 76: Line 111:


====&lt;a>====
====&lt;a>====
  data-role="['''button'''|...]"
href="..." // for page transitions see [[#Page_Transition|here]]
  data-icon="['''home'''|'''grid'''|'''info''']"
  data-role="['''button'''|...]" // see [[#data-role|here]]
  data-icon="['''home'''|'''grid'''|'''info''', ...]" // see https://api.jquerymobile.com/icons/
  data-iconpos="notext"
  data-iconpos="notext"
  data-transition="slidedown" // defines the optical transition to another page
  data-transition="slidedown" // defines the optical transition to another page
  data-rel="dialog"
  data-rel="dialog"
  data-inline="true"
  data-inline="true"
  rel="['''external'''|...]"
  rel="['''external'''|...]" // these links will cause a full page refresh with no animated transition.


====&lt;select>====
====&lt;select>====
Line 92: Line 128:


===API===
===API===
The '''$''' sign is special and important charactor. $() is used for element selection like:
The '''$''' sign is special and important character. $() is used for element selection like:
  $(this).hide() - hides the current element.
  $(this).hide() - hides the current element.
   
   
Line 107: Line 143:


====ajax()====
====ajax()====
There are two version ajax(url,options) and ajax(options) ([http://api.jquery.com/jquery.ajax/ offical doc]), but in most cases the latter is used. Options is an array of key value pairs:
  $.ajax({
  $.ajax({
   url: url,
   url: url,
   dataType: 'json', //json data type
   dataType: 'json', // type of data that you're expecting back from the server
   data: data,
   data: data, // data to be sent to the server
   success: callback,
   success: callback, // has 3 parameter: data returned from server of type 'dataType', status string, XMLHttpRequest
   error: callback
   error: function (jqXHR, textStatus, errorThrown) // errorThrown is the text equivalent for the return code
  });
  });


Line 127: Line 165:
   
   
is equivalent to [[#ajax.28.29|ajax()]].
is equivalent to [[#ajax.28.29|ajax()]].
==Patterns==
===Implement a dialog===
* see UweHeuer app -> mobile.html and MobileController.js for ajax errors
===Link, Hints and Dialogs===
* http://stackoverflow.com/questions/25364868/jqm-taphold-event-does-not-prevent-click-event


==Resources==
==Resources==
* [http://emea.nttdata.com/blog/de/2013/07/17/eine-mobile-app-mit-html5-erstellen-vieles-klappt-manches-nicht/ personal account]
* [http://emea.nttdata.com/blog/de/2013/07/17/eine-mobile-app-mit-html5-erstellen-vieles-klappt-manches-nicht/ Eine mobile App mit HTML5 erstellen: Vieles klappt, manches nicht]
* [http://www.w3schools.com/jquerymobile/jquerymobile_examples.asp jQuery mobile examples]
* [http://www.w3schools.com/jquerymobile/jquerymobile_examples.asp jQuery mobile examples]

Latest revision as of 22:01, 14 January 2019

Concepts[edit]

JQuery is a JavaScript library. With jQuery you select (query) HTML elements and perform actions on them. Basic syntax is:

$(selector).action()

All selectors in jQuery start with the dollar sign and parentheses:

$(<Selector>)

There are different Selectors:

  1. id selector like $("#id")
  2. element selector like $("p")
  3. class selector like $(".classname")
  4. ... (complete list see here)

There are multiple event methods to react on specific events like

$("p").click(function(){
  $(this).hide();
});

Libraries[edit]

In order to use JQuery and JQuery Mobile include:

<meta name="viewport" content="width=device-width, initial-scale=1"> 
<link rel="stylesheet" href="resources/css/jquery.mobile-x.x.x.min.css" />
<script src="https://code.jquery.com/jquery-x.x.x.min.js" type="text/javascript"></script>
// event handler for mobileinit event
<script src="resources/js/libs/jquery.mobile-x.x.x.js" type="text/javascript"></script>

Mobile Pages[edit]

jQuery Mobile is a touch-optimized web framework for creating mobile web applications. jQuery Mobile is built on top of the jQuery library. jMobile web pages consist of different pages (data-role="page") regularly as part of a single HTML file and each with a header, content and footer section:

<div data-role="page" id="p1" data-title="Title">

<div data-role="header" id="p1">... <div data-role="content" id="p1">... <div data-role="footer" id="p1">...

</div> 
<div data-role="page" id="p2"> 
  ...
</div>

Page Transition[edit]

Transition to another page is often implementet as

// html
<a href="#<PageID>">...</a> // page inside the same html
<a href="xyz.html">...</a> // new html page loaded via Ajax
<a href="" data-rel="back">...</a> // back button

// javascript
$.mobile.changePage(<PageID>);
window.location.href = "mobile.html";

When the HTML file is loaded, the first page in the body of the file is displayed. If the application is split to multiple html pages, first page is loaded normally.

Passing Data between Pages[edit]

There are different methods:

  • because everything is loaded during the HTML file load one option is a global data object in the global namespace
  • to get data from the previous page you can register a function for the pagebeforechange event, the data parameter has a prevPage and a toPage member

Templates[edit]

A good reference can be found here:

<script id="bookTemplate" type="text/x-jQuery-tmpl">

Events[edit]

In JQuery (not in JQM, see pageinit event below) when the DOM is loaded completely the document.ready() function is called therefore any initialization should go here:

$(document).ready(function() {
  ...
});

As of version 1.7 bind an event to a function by the on() function , which replaces all old binding types:

$( "#members li a" ).on( "click", function( e ) {} );  

Obsolete:

$(document).bind("mobileinit", function () { Notes.controller.init(); });
$("p").click(function() { ... });
$("p").hover(function() { ... });
$( "#members li a" ).live( "click", function( e ) {} );
$( "#members" ).delegate( "li a", "click", function( e ) {} );

JQuery Mobile Events[edit]

jQuery Mobile offers several custom events that build upon native events to create useful hooks for development. The sequence can be found here and a concept description here:

  • mobileinit // any event handler ($(document.bind("mobileinit", ...)hast to be defined before JQM include because it is fired immediately
  • pagechange
  • pageinit: in jQuery Mobile, Ajax is used to load the content of each page into the DOM as you navigate.Because of this, $(document).ready() will/may trigger before your first page is loaded and every code intended for a page manipulation will execute only after a page refresh
  • swipe
  • swipeleft
  • swiperight
  • tap
  • taphold

Icons[edit]

data roles[edit]

  • uses the data-role attribute

button role[edit]

content role[edit]

Obviously just convention without impact.

controlgroup role[edit]

footer role[edit]

data-position="fixed"

header role[edit]

listview role[edit]

data-inset="true" // creates margins around the list within a content area

navbar role[edit]

page role[edit]

data-theme="[a|b|c]" define standard colors and fonts

HTML Tag extensions[edit]

<a>[edit]

href="..." // for page transitions see here
data-role="[button|...]"  // see here
data-icon="[home|grid|info, ...]" // see https://api.jquerymobile.com/icons/
data-iconpos="notext"
data-transition="slidedown" // defines the optical transition to another page
data-rel="dialog"
data-inline="true"
rel="[external|...]" // these links will cause a full page refresh with no animated transition.

<select>[edit]

data-native-menu=[true|false]" // enables self customization of select optic

<ul>[edit]

data-role="listview"
data-filter="true" // makes the list searchable

API[edit]

The $ sign is special and important character. $() is used for element selection like:

$(this).hide() - hides the current element.

$("p").hide() - hides all p elements.

$(".test").hide() - hides all elements with class="test", class names are prefixed with .
 
$("#test").hide() - hides the element with id="test", id names are prefixed with #

$("p:first") - selects the first p element
  • getting attributes of elements: $('<element name>').attr('<attribute name>')
  • setting attributes of elements: $('a').attr({<attribute name>: '<value>', <attribute name>: '<value>'});

ajax()[edit]

There are two version ajax(url,options) and ajax(options) (offical doc), but in most cases the latter is used. Options is an array of key value pairs:

$.ajax({
  url: url,
  dataType: 'json', // type of data that you're expecting back from the server
  data: data, // data to be sent to the server
  success: callback, // has 3 parameter: data returned from server of type 'dataType', status string, XMLHttpRequest
  error: function (jqXHR, textStatus, errorThrown) // errorThrown is the text equivalent for the return code
});

returns a jQuery XMLHttpRequest (jqXHR) object.

jQuery()[edit]

  • $() is short of jQuery()


getJSON()[edit]

$.getJSON('car-sale-report.json', function() {
  //callback
});

is equivalent to ajax().

Patterns[edit]

Implement a dialog[edit]

  • see UweHeuer app -> mobile.html and MobileController.js for ajax errors

Link, Hints and Dialogs[edit]

Resources[edit]