Skip to content

Instantly share code, notes, and snippets.

@imlucas
Created April 9, 2015 01:00
Show Gist options
  • Save imlucas/61164907bc76b195e13c to your computer and use it in GitHub Desktop.
Save imlucas/61164907bc76b195e13c to your computer and use it in GitHub Desktop.
var AmpersandCollection = require('ampersand-collection'),
_ = require('underscore'),
AmpersandRestCollection = _.clone(require('ampersand-collection-rest-mixin')),
sync = require('./sync'),
Search = require('./search'),
EJSON = require('mongodb-extended-json'),
AmpersandModel = require('ampersand-model');
var MongoDBCollection = AmpersandCollection.extend(AmpersandRestCollection, {
fetched: false,
sync: function() {
return sync.json.apply(this, arguments);
},
needsFetch: function(){
return this.fetched !== true;
},
fetch: function(options){
options = options || {};
options.data = this.search.serialize();
this.fetched = true;
this.totaler.fetched = false;
this.totaler.__fetch(options);
return AmpersandRestCollection.fetch.call(this, options);
}
});
Object.defineProperties(MongoDBCollection.prototype, {
search: {
get: function(){
var collection = this;
if(!collection._search){
collection._search = new Search();
}
return collection._search;
}
},
query: {
get: function(){
return this.search.query;
},
set: function(newVal){
if(_.isString(newVal)){
newVal = JSON.parse(newVal);
}
this.search.query = EJSON.deflate(newVal);
}
},
fields: {
get: function(){
return this.search.fields;
},
set: function(newVal){
this.search.fields = EJSON.deflate(newVal);
}
},
_sort: {
get: function(){
return this.search.sort;
},
set: function(newVal){
if(_.isString(newVal)){
newVal = EJSON.deflate(newVal);
}
if(_.isString(newVal)){
var data = {};
data[newVal] = -1;
newVal = data;
}
this.search.sort_field = _.keys(newVal)[0];
this.search.sort_direction = _.values(newVal)[0] === -1 ? 'desc' : 'asc';
}
},
skip: {
get: function(){
return this.search.skip;
},
set: function(newVal){
this.search.skip = newVal;
}
},
limit: {
get: function(){
return this.search.limit;
},
set: function(newVal){
this.search.limit = newVal;
}
}
});
var Totaler = AmpersandModel.extend({
props: {
count: {type: 'number', default: 0},
fetched: {type: 'boolean', default: false}
},
// url: '/api/v4/runs/count',
sync: function() {
return sync.json.apply(this, arguments);
},
__fetch: function(options){
this.fetched = true;
return this.fetch(options);
}
});
Object.defineProperties(MongoDBCollection.prototype, {
totaler: {
get: function(){
var collection = this;
if(!collection._totaler){
collection._totaler = new Totaler()
.on('change:count', this.trigger.bind(this, 'change:total'));
collection._totaler.url = function(){
return _.result(collection, 'url') + '/count';
};
}
return collection._totaler;
},
set: function(newVal){
this._totaler = newVal;
}
},
total: {
get: function(){
return this.totaler.count;
},
set: function(newVal){
return this.totaler.count = newVal;
}
}
});
module.exports = MongoDBCollection;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment