1 | var MongoDBInstance = require('./model');
|
2 | var MongoDBCollection = require('mongodb-collection-model');
|
3 | var MongoDBCollectionCollection = require('mongodb-collection-model').Collection;
|
4 | var BaseDatabaseModel = require('mongodb-database-model');
|
5 | var BaseDatabaseCollection = require('mongodb-database-model').Collection;
|
6 | var filterableMixin = require('ampersand-collection-filterable');
|
7 | var toNS = require('mongodb-ns');
|
8 | var raf = require('raf');
|
9 | var result = require('lodash.result');
|
10 | var clone = require('lodash.clone');
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | var MongoDBCollectionOnInstanceCollection = MongoDBCollectionCollection.extend({
|
17 | namespace: 'MongoDBCollectionOnInstanceCollection',
|
18 | model: MongoDBCollection,
|
19 | parse: function(res) {
|
20 | return res.filter(function(d) {
|
21 | return !toNS(d._id).system;
|
22 | });
|
23 | },
|
24 | |
25 |
|
26 |
|
27 |
|
28 |
|
29 | select: function(model) {
|
30 | if (model.selected) {
|
31 | return false;
|
32 | }
|
33 | raf(function selectableMarkModelSelected() {
|
34 | var current = this.selected;
|
35 | if (current) {
|
36 | current.selected = false;
|
37 | }
|
38 | model.selected = true;
|
39 | this.selected = model;
|
40 | this.trigger('change:selected', this.selected);
|
41 | }.bind(this));
|
42 |
|
43 | return true;
|
44 | },
|
45 | unselectAll: function() {
|
46 | if (this.selected) {
|
47 | this.selected.set({
|
48 | selected: false
|
49 | });
|
50 | }
|
51 | this.selected = null;
|
52 | this.trigger('change:selected', this.selected);
|
53 | }
|
54 | }, filterableMixin);
|
55 |
|
56 | var DatabaseModel = BaseDatabaseModel.extend({
|
57 | collections: {
|
58 | collections: MongoDBCollectionOnInstanceCollection
|
59 | }
|
60 | });
|
61 |
|
62 | var DatabaseCollection = BaseDatabaseCollection.extend({
|
63 | model: DatabaseModel
|
64 | }, filterableMixin);
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | module.exports = MongoDBInstance.extend({
|
73 | namespace: 'MongoDBInstance',
|
74 | collections: {
|
75 | databases: DatabaseCollection,
|
76 | collections: MongoDBCollectionOnInstanceCollection
|
77 | },
|
78 | url: '/instance',
|
79 | fetch: function(options) {
|
80 | var model = this;
|
81 | var url = result(model, 'url');
|
82 |
|
83 | options = options ? clone(options) : {};
|
84 | if (!options.parse) {
|
85 | options.parse = true;
|
86 | }
|
87 |
|
88 | var success = options.success;
|
89 | options.success = function(resp) {
|
90 | if (!model.set(model.parse(resp, options), options)) {
|
91 | return false;
|
92 | }
|
93 | if (success) {
|
94 | success(model, resp, options);
|
95 | }
|
96 | model.trigger('sync', model, resp, options);
|
97 | };
|
98 |
|
99 | var fn = options.error;
|
100 | options.error = function(resp) {
|
101 | if (fn) {
|
102 | fn(model, resp, options);
|
103 | }
|
104 | model.trigger('error', model, resp, options);
|
105 | };
|
106 |
|
107 | var done = function(err, res) {
|
108 | if (err) {
|
109 | return options.error({}, 'error', err.message);
|
110 | }
|
111 | raf(function onClientSuccess() {
|
112 | options.success(res, 'success', res);
|
113 | });
|
114 | };
|
115 |
|
116 | options.dataService.instance({}, done);
|
117 | }
|
118 | });
|