Giter Site home page Giter Site logo

couchito's Introduction

Couchito.js

This is an ongoing project to make the Couchbase Lite usage easier in Phonegap apps.

I used the EventTarget class in order to add events support to custom JavaScript objects and, at the same time, keep the example as simple as I can. You're free to change the classes and use any other library.

This work is based on the projects CouchChat and TodoLite.

Preconditions

  1. Create a phonegap project.
  2. Copy the SyncManager.js, Couch.js, coax.js and EventTarget.js files into www directory.
  3. Include those files in the index.html.

If you use a library that already has support for events in JavaScript, then don't include EventTarget.js. You'll need to modify the code to get rid of EventTarget.

Couch class

Couch uses internally two instances of a sync manager, one for pulling changes and another one for pushing them to the Sync Gateway.

Example

var couchito = new Couch();

couchito.init(dbname);

// Starts up the sync process.
couchito.sync(email, password, function () {
	// ...
});

couchito.addListener('pull.synched', function () {    
    console.log('pull.synched event!');
    // ...
});

// Creates a new document.
var doc = {
    title: 'foo',
    type: 'news',
    text: 'bar'
};

couchito.post(
    {
        doc: doc
    }, 
    function (err, newDoc) {
        if (err) {
            console.error('Error: ', JSON.stringify(err));
            return;
        }

        console.log('resp: ', JSON.stringify(newDoc));

        // Sets up the new id and rev_id.
        doc._id = newDoc.id;
        doc._rev = newDoc.rev;
    }
);

// Gets a doc.
couchito.get({ doc: id }, callback);

// Updates a doc.
couchito.put({ doc: doc }, callback);

SyncManager class

This class is used to start the replication (pull or push, or both) process.

Example

var pull = new SyncManager({ 
	target: 'dbname', 
	source: { 
		url: 'http://user:[email protected]:4984/dbname'
	}, 
	continuous: true 
});

var push = new SyncManager({ 
	source: 'dbname', 
	target: { 
		url: 'http://user:[email protected]:4984/dbname'
	}, 
	continuous: true 
});


pull.addListener('started', function () {
	console.log('Pull sync has started.');
});

pull.addListener('error', function (err) {
	console.error('Replication error: ', JSON.stringify(err));
});

pull.addListener('synched', function () {
	console.log('Pull sync finished');
});


pull.start();

pull.waitForSync(function () { 
	console.log('Pull sync finished.');

	push.start();
});

TODO

  • Add a class as a wrapper to make the HTTP requests (implement strategy pattern maybe), in order not to depend on coax library.

  • Fire an event when detect new changes in the db.

couchito's People

Contributors

fedher avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

lulu523 cfontana0

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.