Book Image

Offline First Web Development

By : Daniel Sauble
Book Image

Offline First Web Development

By: Daniel Sauble

Overview of this book

When building mobile apps, it’s easy to forget about the moments when your users lack a good Internet connection. Put your phone in airplane mode, open a few popular apps, and you’ll quickly see how they handle being offline. From Twitter to Pinterest to Apple Maps, some apps might handle being offline better—but very few do it well. A poor offline experience will result in frustrated users who will abandon your app, or worse, turn to your competitor’s apps Expert or novice, this book will teach you everything you need to know about designing and building a rigorous offline app experience. By putting the offline experience first, you’ll have a solid foundation to build upon, avoiding the unnecessary stress and frustration of trying to retrofit offline capabilities into your finished app. This basic principle, designing for the worst-case scenario, could save you countless hours of wasted effort.
Table of Contents (17 chapters)
Offline First Web Development
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Connecting to our Web API


With PouchDB as our offline database of choice, let's switch over from LocalStorage and connect it to our remote CouchDB database. We'll need to download PouchDB, add it to our Sencha Touch app, and configure it properly.

Adding PouchDB to the app

First, download PouchDB and copy it to your app folder:

  1. Open www.pouchdb.com in your browser.

  2. Click Download at the top of the page.

  3. Copy this file (named pouchdb-3.6.0.min.js or similar) to the todo-app/ folder.

Next, edit app.json and add this file to the js array:

{
  "path": "pouchdb-3.6.0.min.js"
}

Finally, add an init function to the Item store, where you'll initialize both the local and remote PouchDB databases. The local database will use LocalStorage and remote database will use the Cloudant database that we created in the previous chapter:

remoteDB: null,
localDB: null,
initialize: function() {
  var me = this;

  me.remoteDB = new PouchDB('https://username:[email protected]/lists');
  me.localDB = new PouchDB...