• No se han encontrado resultados

Aplicación del Método a RUP (Proceso Unificado del Rational)

implementación

Dimensión 1 Alcance de la metodología

3.2.4. Aplicación del Método a RUP (Proceso Unificado del Rational)

Specifying that a page should be available for offline use is as easy as adding an attribute to the HTML element in the page. Here’s an example:

<html manifest="webApp.appcache">

</html>

The manifest attribute on the html element tells the browser that this webpage needs to be available offline. The value of the manifest attribute points to a manifest file. The name of the file is a convention more than a requirement; you can name the file anything, but the file extension is usually .appcache.

ptg14200515 EXAM TIP

If you really want to change the file extension, you need to configure the web server so that your chosen file extension is returned with a MIME type of text/cache-manifest.

The application cache manifest file must list each and every file and resource required to be stored for offline use. When the browser parses the manifest attribute of the html element, it downloads the manifest and stores it locally. It also ensures that it downloads all the files listed in the manifest so that they are available offline. The manifest file contains three sec-tions: CACHE, NETWORK, and FALLBACK. Each section might appear just once, multiple times in the file, or not at all. Each serves a specific purpose in how application caching functions when dealing with the resources in specific scenarios. A typical manifest file looks like this:

CACHE MANIFEST

# My Web Application Cache Manifest

# v.1.0.0.25

#

#Cache Section. All Cached items.

CACHE

/pages/page1.html /pages/page2.html

#Required Network resources NETWORK:

login.html

#Fallback items.

FALLBACK:

login.html fallback-login.html

The first line in a manifest file must be CACHE MANIFEST. The manifest file, as with any code file, can have comment lines added to it for additional explanations, as denoted by the

# symbol. The CACHE section lists all the resources that must be cached offline. This must include all CSS files, JPG files, video and audio files, and any other resource required for the page to function correctly. If you omit an item from the manifest file, it won’t be cached, which can result in unexpected behavior when the application is run offline.

The NETWORK section declares any resources that must be available from the Internet.

These items can’t be cached. Anything that the page requires from the Internet, such as embedded third-party elements, must be listed here. If such a resource isn’t listed here, the browser won’t know to check on the Internet for it when in offline mode. When the browser is in offline mode, it doesn’t attempt to go to the Internet for anything unless it’s listed in the NETWORK section.

The FALLBACK section enables you to provide fallback instructions to the browser in the event that an item isn’t available in the cache and the browser is in offline mode. In the

ptg14200515 example file, if login.html isn’t available in the cache, render fallback-login.html. You can use

shortcuts in the FALLBACK section to provide more general redirects, such as the following:

/resources /resource.jpg

This tells the browser that if the browser is offline and can’t access anything in the resources folder, it should replace any references to items in the resources folder with resource.jpg. Note that resource.jpg is cached because it’s specified in the FALLBACK section.

You don’t need to also specify resource.jpg in the CACHE section.

Using the AppCache API

As with Web Storage, the application cache is available in JavaScript as a global object. The following code gets a reference to the AppCache global object:

var appCache = window.applicationCache;

When you’re using the application cache to make pages available offline, one of the more useful things you can do when the page is loaded is verify its status. You achieve this by evaluating the status property of the AppCache object. The status property could be one of the values listed in Table 1-14.

TABLE 1-14 The application cache status property

Status Description

Uncached The web application isn’t associated with an application manifest.

Idle The caching activity is idle, and the most up-to-date copy of the cache is being used.

Checking The application manifest is being checked for updates.

Downloading The resources in the application manifest are being downloaded.

UpdateReady The resources listed in the manifest have been successfully downloaded.

Obsolete The manifest can no longer be downloaded, so the application cache is being deleted.

After you know the cache status, two methods on the AppCache object can be useful.

Table 1-15 lists these.

TABLE 1-15 Methods available with the applicationCache object

Method Description

swapCache Indicates that the cache be replaced with a newer version.

update Tells the browser to update the cache if an update is available.

ptg14200515 When the update method is called, an update to the cache is prepared. When that’s ready

to download, the status of the application cache changes to UpdateReady. When this is set, a call to the swapCache method tells the application to switch to the most recent cache.

EXAM TIP

The call to the update method is asynchronous. Therefore, you must handle the

onupdateready event to determine when the update has completed the download process.

In addition to the properties and methods, the AppCache object can raise a series of events that you can handle. The application cache typically operates in the background, and you won’t need these events. However, in some cases handling some of the events and forc-ing an update can be useful. Table 1-16 lists the available events.

TABLE 1-16 Events available from the applicationCache object

Event Description

onchecking The browser is checking for an update to the application manifest, or the application is being cached for the first time.

onnoupdate The application manifest has no update available.

ondownloading The browser is downloading what it has been told to do per the manifest file.

onprogress Files are being downloaded to the offline cache. This event fires periodically to report progress.

oncached The download of the cache has completed.

onupdateready The resources listed in the manifest have been newly redownloaded, and the swapCache method might be called.

onobsolete A manifest file is no longer available.

onerror An error has occurred. This could result from many things. Appropriate logging is necessary to get the information and resolve.

Most of these events might not be used often, if at all. The most common scenario is to handle the onupdateready method and then make a call to the swapCache method, as in this example:

window.onload = function () {

var appCache = window.applicationCache;

appCache.oncached = function (e) { alert("cache successfully downloaded."); };

appCache.onupdateready = function (e) { appCache.swapCache(); };

}

Using the application cache is more about configuration than about coding. However, it’s important that you’re aware the API is available for advanced scenarios where you need more control over the process, or when you need to receive timely information about the process, such as by handling the onprogress event.

ptg14200515