diff --git a/addon/adapters/hoodie.js b/addon/adapters/hoodie.js index e9185ce..402edd7 100644 --- a/addon/adapters/hoodie.js +++ b/addon/adapters/hoodie.js @@ -2,7 +2,13 @@ import DS from 'ember-data'; import Ember from 'ember'; const { - inject: { service } + get, + inject: { + service + }, + String: { + camelize + } } = Ember; export default DS.Adapter.extend({ @@ -15,22 +21,25 @@ export default DS.Adapter.extend({ findRecord(store, type, id) { return this._next(() => { - return this._storeForType(type).find(id); + return this._hoodieStore().find(id); }); }, createRecord(store, type, snapshot) { var props = this.serialize(snapshot); - if (snapshot.id) { props.id = snapshot.id; } + if (snapshot.id) { + props.id = snapshot.id; + } + props.type = camelize(type.modelName); return this._next(() => { - return this._storeForType(type).add(props); + return this._hoodieStore().add(props); }); }, updateRecord(store, type, snapshot) { var props = this.serialize(snapshot); return this._next(() => { - return this._storeForType(type).update(snapshot.id, props); + return this._hoodieStore().update(snapshot.id, props); }); }, @@ -40,13 +49,16 @@ export default DS.Adapter.extend({ // https://github.com/hoodiehq/hoodie-store-client/issues/95 deleteRecord(store, type, snapshot) { return this._next(() => { - return this.get('hoodie.store').remove(snapshot.id); + return this._hoodieStore().remove(snapshot.id); }); }, findAll(store, type) { return this._next(() => { - return this._storeForType(type).findAll(); + let isOfType = function(m){ + return m.type === type.modelName; + }; + return this._hoodieStore().findAll(isOfType); }); }, @@ -54,8 +66,8 @@ export default DS.Adapter.extend({ throw new Error('not implemented'); }, - _storeForType(type) { - return this.get('hoodie.store')(type.modelName); + _hoodieStore() { + return get(this, 'hoodie.store'); }, // This is used to synchronize and single-file hoodie operations. diff --git a/addon/services/hoodie-account.js b/addon/services/hoodie-account.js index 7902299..636a345 100644 --- a/addon/services/hoodie-account.js +++ b/addon/services/hoodie-account.js @@ -1,9 +1,9 @@ import Ember from 'ember'; - const { - computed, get, - inject: { service } + inject: { + service + } } = Ember; // I kinda just guessed at these... @@ -28,6 +28,8 @@ const proxiedFunctions = [ export default Ember.Service.extend({ hoodie: service(), + // isSignedIn: false, + // username: '', init() { this._super(...arguments); @@ -38,15 +40,40 @@ export default Ember.Service.extend({ let props = eventInvalidations[event]; account.on(event, this._notifyProperties.bind(this, props)); } + this.set('account', account); + + let update = () => { + this._updateAccountProperties(); + }; + + this._updateAccountProperties(); + + this.get('hoodie.account').on('signin', update); + this.get('hoodie.account').on('signout', update); + this.get('hoodie.account').on('unauthenticate', update); + this.get('hoodie.account').on('reauthenticate', update); + this.get('hoodie.account').on('update', update); }, - isSignedIn: computed(function() { - return get(this, 'hoodie.account').isSignedIn(); - }), + _updateAccountProperties: function() { + this.account.get('session').then(session => { + let signedIn = session ? true : false; + this.set('isSignedIn', signedIn); + }); + this.account.get('username').then(username => { + this.set('username', username); + }); + this.account.get('session.invalid').then(hasInvalidSession => { + this.set('hasInvalidSession', hasInvalidSession); + }); + this.account.profile.get().then(profile => { + this.set('profile', profile); + }); + }, - username: computed(function() { - return get(this, 'hoodie.account.username'); - }), + _hoodieAccount() { + return get(this, 'hoodie.account'); + }, _notifyProperties(props) { props.forEach((prop) => { diff --git a/addon/services/hoodie.js b/addon/services/hoodie.js index 050dac5..90dc12e 100644 --- a/addon/services/hoodie.js +++ b/addon/services/hoodie.js @@ -1,6 +1,5 @@ -/* globals Hoodie */ +/* globals Hoodie, PouchDB */ import Ember from 'ember'; - const { Service, get, @@ -11,8 +10,10 @@ export default Service.extend({ init() { this._super(...arguments); const appConfig = Ember.getOwner(this).application.resolveRegistration('config:environment'); - const hoodieConfig = appConfig.hoodie ? appConfig.hoodie.client : {}; - const hoodie = new Hoodie(hoodieConfig); + const hoodie = new Hoodie({ + url : appConfig.hoodie.client.url, + PouchDB : PouchDB + }); set(this, 'hoodie', hoodie); // for debug only window.hoodie = hoodie; diff --git a/index.js b/index.js index d859d6f..3ca6c72 100644 --- a/index.js +++ b/index.js @@ -19,19 +19,31 @@ function hoodieMiddleware(config) { } var Hapi = require('hapi'); - var hoodie = require('hoodie'); + var hoodie = require('hoodie').register; var proxy = require('http-proxy-middleware'); + var PouchDB = require('pouchdb'); config.app.use('/hoodie', proxy({target: 'http://localhost:' + appConfig.hoodie.server.port})); var server = new Hapi.Server(); server.connection({ + host: 'localhost', port: appConfig.hoodie.server.port }); server.register({ register: hoodie, - options: appConfig.hoodie.server + options: { + PouchDB: PouchDB, + // paths: { + // data: '.hoodie' + // }, + adminPassword: appConfig.hoodie.server.adminPassword, + client: { + url: "http://localhost:"+ appConfig.hoodie.server.port + } + } + // options: appConfig.hoodie.server }, function (error) { if (error) { throw error; @@ -60,8 +72,13 @@ module.exports = { srcDir: 'dist', destDir: '/' }); + var pouchdbPackage = path.join(path.dirname(require.resolve('pouchdb')), '..', 'dist'); + var pouchdbTree = new Funnel(pouchdbPackage, { + files: ['pouchdb.js'], + destDir: 'pouchdb' + }); if (tree) { - return mergeTrees([tree, hoodieTree]); + return mergeTrees([tree, hoodieTree, pouchdbTree], {overwrite: true}); } else { return hoodieTree; } @@ -71,6 +88,7 @@ module.exports = { this._super.apply(this, arguments); app.import('vendor/hoodie.js'); + app.import('vendor/pouchdb/pouchdb.js'); }, serverMiddleware: hoodieMiddleware