Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View pouchcore.coffee
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
class PouchCore
constructor: (@remoteUrl,@onChange)->
if @remoteUrl.slice(0,4)=="http" #did we get a real url?
parts = @remoteUrl.split("/") #split the url bu by the slashes
@_dbName = parts.pop() #assign the last part as the db name
while @_dbName == "" #unless it is an empty string
@_dbName = parts.pop()#repeat until you find one
Pouch @_dbName, (e, db) => #making the local db
unless e #error would imply we are on an old browser
@db = db
@db.changes(
continuous : true
include_docs : true
onChange : @onChange
)
#we replicate
_to=@db.replicate.to @remoteUrl,{continuous: true}
_from=@db.replicate.from @remoteUrl, {continuous: true}
@up=true
@stop = ()=>
console.log "stopped"
@up=false
_to.cancel()
_from.cancel()
@
@start = ()=>
unless @up
@up=true
_to=@db.replicate.to @remoteUrl,{continuous: true}
_from=@db.replicate.from @remoteUrl, {continuous: true}
@
@
else #there was an error lets try again but just with the remote one
Pouch @remoteUrl, (e, db) =>
unless e
@db
@db.changes(
continuous : true
include_docs : true
onChange : @onChange
)
@
else
return "yeah something went wrong"
add: (doc, cb = ()-> true) ->
unless "_id" of doc
@db.post doc, cb
else if "_id" of doc and doc._id.slice(0,8) != "_design/"
@db.put doc, cb
else if doc.length
@db.bulkDocs doc, cb
get: (id, cb = ()-> true) ->
@db.get id, cb
remove: (id, cb = ()-> true) ->
@get id, (err, doc) =>
@db.remove doc, cb unless err
cb("err") if err
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.