Unit 4
Unit 4
Client-side basics
Application Sources
initOptions.js
It defines application initialization options.
The initOptions.js file is included in the project template. It is used to initialize the Worklight
JavaScript framework. It contains a number of tailoring options, which you can use to change
the behavior of the JavaScript framework.
The initOptions.js file calls WL.Client.init, passing an options object that includes any values that
you have overridden.
jQuery JavaScript framework
wlq.js
Worklight includes an encapsulated jQuery, which can be added to the developer project with a
single line of code: – window.$ = window.jQuery = WLJQ;
The reference for the encapsulated jQuery in Worklight that is shown above, must be placed in
the appName.js file. There is no error message, or problem indicated if you do not include this
line.
Worklight client JavaScript API
Worklight.js
Worklight Client API uses WL namespace. It provides bridging to native mobile platform APIs,
dynamic HTML load, and so on.
The WL namespace
• Use a WL namespace to use a Worklight API, for example: WL.Client, WL.Utils, and so
on.
• Exposes the API objects, methods, and constants (usually enums).
• Automatically added to the application main HTML file – worklight.js
• WL Namespace is automatically available on application initialization.
WL.client
With WL.Client, you can perform the following type of tasks:
• Initialize and reload the app
• Trigger login and logout
• Obtain general app information
• Retrieve and update data from corporate information systems
WL.Client.invokeProcedure (invocationData, options)
• Store and retrieve user preferences across sessions
• Specify environment-specific user interface behavior
• Store custom log lines for auditing and reporting purposes in special database tables
WL.Client.logActivity (activityType)
Basic Debugging
Client-side debugging
You can debug native code by using standard tools that the platform SDK provides, such as
Xcode, Android LogCat/ADB, or Microsoft Visual Studio.
Desktop browser
Modern browsers, such as Chrome, Firefox, Safari, Opera, or Internet Explorer, provide an easy
and convenient way to debug web apps.
Web tools
There are many web tools for debugging on various desktop browsers, such as:
• FireBug extension for Firefox
• Developer tools for Chrome (press Ctrl+Shift+I to start)
• Developer tools for Internet Explorer (press F12 to start)
• Dragonfly for Opera
WL.Logger
IBM Worklight provides a WL.Logger object that you use to print log messages to the log for the
environment used.
Two of its methods: WL.Logger.debug() and WL.Logger.error()
The APIs are multiplatform. The output destination changes according to the platform on which
that application runs on:
• Developer console when it is running on a desktop browser.
• LogCat when it is running on Android device.
• Visual Studio Output when it is running on a Windows Phone 8 device and Windows 8
Apps.
• Xcode console when it is running on an iOS device.
Weinre (Web Inspector Remote)
Weinre is a debugger for web pages that is designed to work remotely.
You can use Weinre:
• for debugging of web pages on mobile devices such as iPhone or Android.
• to inspect and debug web resources such as HTML, JavaScript, CSS, and network traffic
on a remote mobile handset.
Weinre has a built-in JavaScript console that you can use as an extra powerful tool for web-
based mobile application debugging.
WireShark
It is a network protocol analyzer that you can use to see what happens on your network at a
microscopic level.
With WireShark, you can filter the range of traffic that you want to follow.
Note:
An IBM Worklight hybrid application uses a single DOM model. A single DOM model means that
you cannot navigate between various HTML files by using hyperlinks or changing the
window.location property.
Instead, you must implement multi-page interfaces by loading external HTML file content, and
by using Ajax requests and injecting into the existing DOM. This is because the main application
HTML file loads the IBM Worklight client-side JavaScript framework files, and when the browser
navigates from one HTML file to another, the JavaScript context and loaded scripts are lost.
Design Considerations
• A single HTML file is the preferred model for simpler applications.
• The developer is responsible for showing the current page <div> and hiding the rest.
However, large applications present a challenge:
• The developer must take full responsibility for which DIVs are shown and which DIVs are
hidden at any moment.
• If you want to add some new content to a page, for example a table, you cannot load a
prepared template but must generate it manually.
• A single large HTML file with many pages takes longer to load.
• It is easy to get lost in a single HTML file that contains multiple pages. Separate files are
easier to manage.
Dynamic Page Loading
Building a rich dynamic application with multiple pages can be easier with dynamic pages
loading.
• You can use built-in jQuery APIs to dynamically load, update, and insert DOM elements
in your application.
• You can insert HTML pages with CSS and JavaScript as needed.
• It is possible to implement navigation history.
• JavaScript code can be run when pages are loaded or unloaded
Loading an external HTML file
An external HTML file is a segment of HTML code that can be injected into any location in the
existing DOM.
• Can include JavaScript by using <script> tag
• Can include CSS files by using <link> tag
• You can inject it into the parent element by using <div>, but this method is not
mandatory
• Implemented by using jQuery $().load()API
• To load an HTML file, use the following syntax:
$(containerSelector).load(filePath, callbackFunction);
NOTE:
containerSelector: jQuery css selector of element to host the loaded content.
filePath: Relative path to an HTML file. Always relative to main HTML file.
callbackFunction: JavaScript function to execute when loading completes.
Common Controls
Worklight common controls are sets of pre-built client-side libraries or UI widgets for frequently
used UI functions such as modal pop-up, loading screens, tab bars, and other functions. These
UI components are common to most device environments.
Worklight provides a JavaScript API to invoke these controls regardless of the environment. It
automatically renders them in a native way for each mobile platform.
Developers can save a lot of time because they do not have to build these UI component
controls. They customize them to render in a native fashion for each supported device
environment.
WL.BusyIndicator
• WL.BusyIndicator implements a common API to display a modal activity indicator.
• Uses native implementation on Android, iPhone, and Windows Phone platforms.
• Must be initialized before use.
• The parent element ID for WL.BusyIndicator is ignored in iOS, Android, Windows Phone,
and BlackBerry environments.
• Available options are:
▪ text: set the modal text
▪ color: set the modal text
▪ fullScreen: use if modal message should be displayed full screen (iOS only)
• WL.BusyIndicator provides the following API:
▪ Initialization
▪ void myBusyIndicator.show(): displays busy indicator
▪ void myBusyIndicator.hide(): hides busy indicator
▪ boolean myBusyIndicator.isVisible(): returns whether busy indicator is visible
WL.SimpleDialog
• Implements a common API for showing a dialog box with buttons for the application.
This technology is useful in constructing informational pop-ups or getting the user’s
attention.
• Uses native implementation on mobile platforms.
• Supports up to three buttons.
• Invocation syntax is:
▪ Parameters: title, text, and buttons as an array of up to three button objects
▪ The dialog box is closed when any of the buttons is clicked
▪ Options is an object with the form {title:String, text: String}
Worklight SimpleDialog takes four parameters:
1. The first parameter defines the title of the dialog box. It is mandatory.
2. The second parameter defines the text to show in the dialog box. It is also mandatory.
3. The third parameter represents the buttons to be placed in the dialog box. You can add
up to three buttons.
4. The options parameter is ignored by iPhone and Android. It has the form {title: string,
text: string}.
The active dialog box is closed when any of the buttons is pressed.
• The options menu displays buttons that can be used to invoke activities that are related
to the primary functionality.
• The options menu can be displayed by:
▪ Android
▪ Windows Phone 8
▪ Windows 8
On Android, the Options menu contains primary functionality that applies globally to the
current activity or starts a related activity. It is typically invoked when the user presses a button,
often labeled Menu.
Offline Access
• Each time the Worklight framework attempts to access the Worklight Server; it might
detect that the application switched from offline to online status or vice versa.
• In each case, JavaScript events are fired
▪ WL.Events.WORKLIGHT_IS_DISCONNECTED event is fired when connectivity to
the Worklight Server fails
▪ WL.Events.WORKLIGHT_IS_CONNECTED event is fired when connectivity to the
Worklight Server is restored.
• Developers can add event listeners to these events and specify the callback functions
that handle them.
connect()
• isAirplaneMode: true/false
• carrierName: string (for example, AT&T, VERIZON, and other providers)
• telephonyNetworkType: string (for example, UMTS or GPRS)
• isRoaming: true/false
• networkConnectionType: mobile/WIFI
• ipAddress: string
• isNetworkConnected: true/false
Foreground event
Worklight heartbeat
String Translation
Introduction to translation
• You can use the Worklight framework to enable translation of applications into other
languages.
• You can set up translations for:
▪ Application strings
▪ System messages
• The Worklight Platform can automatically translate the application strings according to a
designated file.
• Multi-language translation can be implemented by using JavaScript.
Changing the default encoding in Eclipse
• The messages.js file, which is designated for application strings, is in the common/js
folder.
• Application messages that are stored in messages.js can be referenced in two ways:
▪ As a JavaScript object property, for example, Messages.sampleText.
▪ As an ID of an HTML element with class = “translate”.
Translating system messages