Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(app): Move instance from home to app #62

Merged
merged 1 commit into from
Jul 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -90,12 +86,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