forked from balupton/ember-cli-blog
-
Notifications
You must be signed in to change notification settings - Fork 35
/
application.js
70 lines (55 loc) · 2.03 KB
/
application.js
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
58
59
60
61
62
63
64
65
66
67
68
69
70
import config from '../config/environment';
import PouchDB from 'pouchdb';
import { Adapter } from 'ember-pouch';
import { assert } from '@ember/debug';
import { isEmpty } from '@ember/utils';
import { inject as service } from '@ember/service';
export default Adapter.extend({
session: service(),
cloudState: service(),
refreshIndicator: service(),
init() {
this._super(...arguments);
const localDb = config.local_couch || 'blogger';
assert('local_couch must be set', !isEmpty(localDb));
const db = new PouchDB(localDb);
this.set('db', db);
// If we have specified a remote CouchDB instance, then replicate our local database to it
if ( config.remote_couch ) {
const remoteDb = new PouchDB(config.remote_couch, {
fetch: function (url, opts) {
opts.credentials = 'include';
return PouchDB.fetch(url, opts);
}
});
const replicationOptions = {
live: true,
retry: true
};
db.replicate.from(remoteDb, replicationOptions).on('paused', (err) => {
this.cloudState.setPull(!err);
});
db.replicate.to(remoteDb, replicationOptions).on('denied', (err) => {
if (!err.id.startsWith('_design/')) {
//there was an error pushing, probably logged out outside of this app (couch/cloudant dashboard)
this.session.invalidate();//this cancels the replication
throw({message: "Replication failed. Check login?"});//prevent doc from being marked replicated
}
}).on('paused',(err) => {
this.cloudState.setPush(!err);
}).on('error',() => {
this.session.invalidate();//mark error by loggin out
});
this.set('remoteDb', remoteDb);
}
return this;
},
unloadedDocumentChanged: function(obj) {
this.refreshIndicator.kickSpin();
let store = this.store;
let recordTypeName = this.getRecordTypeName(store.modelFor(obj.type));
this.db.rel.find(recordTypeName, obj.id).then(function(doc) {
store.pushPayload(recordTypeName, doc);
});
}
});