Skip to content

Commit

Permalink
fix(app): Move instance from home to app
Browse files Browse the repository at this point in the history
This way, we can easily connect instance details with intercom later
  • Loading branch information
imlucas authored and kangas committed Jul 13, 2015
1 parent 4aac746 commit 5d2dab8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 23 deletions.
53 changes: 43 additions & 10 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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
Expand All @@ -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: {
/**
Expand Down Expand Up @@ -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;
21 changes: 8 additions & 13 deletions src/home/index.js
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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
});
}
Expand All @@ -44,20 +40,20 @@ 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);
});

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();
Expand All @@ -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);
}
Expand All @@ -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;
Expand Down

0 comments on commit 5d2dab8

Please sign in to comment.