1 | var MongoDBCollection = require('./collection-model');
|
2 | var toNS = require('mongodb-ns');
|
3 | var clone = require('lodash.clone');
|
4 | var raf = require('raf');
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | var CollectionModel = MongoDBCollection.extend({
|
11 | namespace: 'MongoDBCollection',
|
12 | session: {
|
13 | selected: {
|
14 | type: 'boolean',
|
15 | default: false
|
16 | }
|
17 | },
|
18 | derived: {
|
19 | name: {
|
20 | deps: ['_id'],
|
21 | fn: function() {
|
22 | return toNS(this._id).collection;
|
23 | }
|
24 | },
|
25 | specialish: {
|
26 | deps: ['_id'],
|
27 | fn: function() {
|
28 | return toNS(this._id).specialish;
|
29 | }
|
30 | },
|
31 | url: {
|
32 | deps: ['_id'],
|
33 | fn: function() {
|
34 | return `/collections/${this.getId()}`;
|
35 | }
|
36 | }
|
37 | },
|
38 | serialize: function() {
|
39 | return this.getAttributes(
|
40 | {
|
41 | props: true
|
42 | },
|
43 | true
|
44 | );
|
45 | },
|
46 | fetch: function(options) {
|
47 | var model = this;
|
48 |
|
49 | options = options ? clone(options) : {};
|
50 | if (!options.parse) {
|
51 | options.parse = true;
|
52 | }
|
53 |
|
54 | var success = options.success;
|
55 | options.success = function(resp) {
|
56 | if (!model.set(model.parse(resp, options), options)) {
|
57 | return false;
|
58 | }
|
59 | if (success) {
|
60 | success(model, resp, options);
|
61 | }
|
62 | model.trigger('sync', model, resp, options);
|
63 | };
|
64 |
|
65 | var fn = options.error;
|
66 | options.error = function(resp) {
|
67 | if (fn) {
|
68 | fn(this, resp, options);
|
69 | }
|
70 | this.trigger('error', this, resp, options);
|
71 | };
|
72 |
|
73 | var done = function(err, res) {
|
74 | if (err) {
|
75 | return options.error({}, 'error', err.message);
|
76 | }
|
77 | raf(function onClientSuccess() {
|
78 | options.success(res, 'success', res);
|
79 | });
|
80 | };
|
81 |
|
82 | options.dataService.collection(this.getId(), options, done);
|
83 | }
|
84 | });
|
85 |
|
86 | module.exports = CollectionModel;
|
87 |
|
88 | module.exports.Collection = MongoDBCollection.Collection.extend({
|
89 | model: CollectionModel
|
90 | });
|