GithubHelp home page GithubHelp logo

pouchdb-ionic's Introduction

pouchdb-ionic

PouchDB runs great in the Ionic Framework. Here's how to get started.

Note: these instructions were written for Ionic 1. For Ionic 2, you might try this tutorial.

Sample app

Installing PouchDB

Install with Bower:

bower install --save pouchdb

Then add pouchdb.js to index.html (after cordova.js):

<script src="cordova.js"></script>

<script src="lib/pouchdb/dist/pouchdb.min.js"></script>

Using PouchDB

PouchDB is already ready to be used within your controllers and services. Just call:

var db = new PouchDB('my_database');

That's it!

Service pattern

Since Angular services are singletons, and since we only need a single instance of a PouchDB database, a good practice is to create a service called PouchService that handles your PouchDB:

function PouchService() {
  this.db = new PouchDB('my_database');
}

angular.module('myModule').service('pouchService', PouchService)

Now you can access your database from pouchService.db.

$q.when()

PouchDB promises are compliant with the A+ spec. So if you want to turn them into Angular $q promises, you just wrap them in $q.when():

$q.when(pouchdb.post({})).then(function () {
  /* ... */
}).catch(function () {
  /* ... */
});

$rootScope.$apply()

Since PouchDB is asynchronous, you will often need to call $scope.$apply() or $rootScope.$apply() in order for the changes to be reflected in your UI. For instance, you might want to do:

function PouchService($scope, $rootScope) {
  $scope.listOfStuff = [];

  $scope.db = new PouchDB('my_database');
  
  $scope.db.allDocs({include_docs: true}).then(function (result) {
    var docs = result.rows.map(function (row) { return row.doc; });
    
    $scope.listOfStuff = docs;
    
    $rootScope.$apply(); // <--- better call this!
    
  }).catch(function (error) {
    // got an error somehow
  });
}

If you get tired of calling $rootScope.$apply() all the time, you may want to look into a wrapper like angular-pouchdb. But it's not required.

NPM Browser

There is an open-source Angular project called NPM Browser that you can check out for a more detailed example of using PouchDB with Angular.

For instance, here is the pouch-service.js in that project.

See also

The pouchdb-phonegap-cordova guide, which has more general information about using PouchDB with Cordova/PhoneGap.

pouchdb-ionic's People

Contributors

nolanlawson avatar

Watchers

James Cloos avatar Ben Keller avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.