From 5d2dab808dffd0116f52db636e7d05063a9ecdf5 Mon Sep 17 00:00:00 2001 From: Lucas Hrabovsky Date: Thu, 9 Jul 2015 10:55:59 -0400 Subject: [PATCH] fix(app): Move `instance` from home to `app` This way, we can easily connect instance details with intercom later --- src/app.js | 53 ++++++++++++++++++++++++++++++++++++++--------- src/home/index.js | 21 +++++++------------ 2 files changed, 51 insertions(+), 23 deletions(-) diff --git a/src/app.js b/src/app.js index 199d8f0d50c..77253d7c206 100644 --- a/src/app.js +++ b/src/app.js @@ -1,12 +1,16 @@ var _ = require('lodash'); var app = require('ampersand-app'); +var pkg = require('../package.json'); var domReady = require('domready'); var qs = require('qs'); var createClient = require('scout-client'); var State = require('ampersand-state'); -var Router = require('./router'); + var QueryOptions = require('./models/query-options'); var Connection = require('./models/connection'); +var MongoDBInstance = require('./models/mongodb-instance'); + +var Router = require('./router'); var Layout = require('./layout'); var Statusbar = require('./statusbar'); var debug = require('debug')('scout-ui:app'); @@ -30,6 +34,12 @@ var debug = require('debug')('scout-ui:app'); * @see http://learn.humanjavascript.com/react-ampersand/application-pattern */ var Application = State.extend({ + props: { + version: { + type: 'string', + default: pkg.version + } + }, children: { /** * @see http://learn.humanjavascript.com/react-ampersand/creating-a-router-and-pages @@ -47,7 +57,11 @@ var Application = State.extend({ * The connection details for the MongoDB Instance we want to/are currently connected to. * @see models/connection.js */ - connection: Connection + connection: Connection, + /** + * Details of the MongoDB Instance we're currently connected to. + */ + instance: MongoDBInstance }, derived: { /** @@ -115,17 +129,36 @@ var Application = State.extend({ } }); -/** - * @todo (imlucas): Figure out why ampersand-app isn't nicer to use out - * of the box with ampersand-state. - */ var params = qs.parse(window.location.search.replace('?', '')); var uri = params.uri || 'mongodb://localhost:27017'; - var state = new Application({ uri: uri }); -app.extend(state); -app.client = state.client; -app.navigate = state.navigate; + +// Copy the instance of `Application` on to the `ampersand-app` instance. +app.extend({ + init: function() { + _.each(_.methods(Application.prototype), function(name) { + this[name] = _.bind(state[name], state); + }, this); + + _.each(_.keys(Application.prototype._children), function(name) { + this[name] = state[name]; + }, this); + + _.each(_.keys(Application.prototype._collections), function(name) { + this[name] = state[name]; + }, this); + + _.each(_.keys(Application.prototype._derived), function(name) { + Object.defineProperty(this, name, { + get: function() { + return state.get(name); + } + }); + }, this); + } +}); +app.init(); + module.exports = window.app = app; diff --git a/src/home/index.js b/src/home/index.js index 3e3cbdb0db5..140addb1ad9 100644 --- a/src/home/index.js +++ b/src/home/index.js @@ -1,16 +1,12 @@ var AmpersandView = require('ampersand-view'); var ViewSwitcher = require('ampersand-view-switcher'); -var MongoDBInstance = require('../models/mongodb-instance'); -var debug = require('debug')('scout-ui:home'); var app = require('ampersand-app'); var format = require('util').format; var SidebarView = require('../sidebar'); var CollectionView = require('./collection'); +var debug = require('debug')('scout-ui:home'); module.exports = AmpersandView.extend({ - children: { - model: MongoDBInstance - }, props: { switcher: { type: 'object', @@ -27,7 +23,7 @@ module.exports = AmpersandView.extend({ deps: ['ns'], fn: function() { if (!this.ns) return null; - return this.model.collections.find({ + return app.instance.collections.find({ _id: this.ns }); } @@ -44,9 +40,9 @@ module.exports = AmpersandView.extend({ options = options || {}; this.ns = options.ns; - app.statusbar.watch(this, this.model); + app.statusbar.watch(this, app.instance); - this.listenTo(this.model, 'sync', function() { + this.listenTo(app.instance, 'sync', function() { if (!this.ns) return; if (!this.currentCollection) return; this.showCollection(this.currentCollection); @@ -54,10 +50,10 @@ module.exports = AmpersandView.extend({ this.listenToAndRun(app.connection, 'change:name', this.updateTitle); this.once('change:rendered', this.onRendered); - this.model.fetch(); + app.instance.fetch(); }, updateTitle: function() { - var model = this.model.collections.selected; + var model = app.instance.collections.selected; var title = app.connection.uri; if (model) { title += '/' + model.getId(); @@ -71,7 +67,7 @@ module.exports = AmpersandView.extend({ }); }, showCollection: function(model) { - var collection = this.model.collections; + var collection = app.instance.collections; if (!collection.select(model)) { return debug('already selected %s', model); } @@ -92,12 +88,11 @@ module.exports = AmpersandView.extend({ subviews: { sidebar: { hook: 'sidebar', - waitFor: 'model.collections', prepareView: function(el) { var view = new SidebarView({ el: el, parent: this, - collection: this.model.collections + collection: app.instance.collections }); view.on('show', this.showCollection.bind(this)); return view;