2017-11-26 23:42:27 +02:00
|
|
|
import {check} from 'meteor/check';
|
2016-11-17 19:54:48 +02:00
|
|
|
|
2017-11-26 23:42:27 +02:00
|
|
|
const storage = '__reducers';
|
|
|
|
Object.assign(Mongo.Collection.prototype, {
|
2016-11-17 19:54:48 +02:00
|
|
|
/**
|
|
|
|
* @param data
|
|
|
|
*/
|
|
|
|
addReducers(data) {
|
|
|
|
if (!this[storage]) {
|
|
|
|
this[storage] = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
_.each(data, (reducerConfig, reducerName) => {
|
2016-11-21 13:28:15 +02:00
|
|
|
if (!this[reducerConfig]) {
|
|
|
|
this[reducerConfig] = {};
|
|
|
|
}
|
|
|
|
|
2016-11-17 19:54:48 +02:00
|
|
|
if (this.getLinker(reducerName)) {
|
|
|
|
throw new Meteor.Error(`You cannot add the reducer with name: ${reducerName} because it is already defined as a link in ${this._name} collection`)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this[reducerConfig][reducerName]) {
|
|
|
|
throw new Meteor.Error(`You cannot add the reducer with name: ${reducerName} because it was already added to ${this._name} collection`)
|
|
|
|
}
|
|
|
|
|
2017-11-26 23:42:27 +02:00
|
|
|
check(reducerConfig, {
|
|
|
|
body: Object,
|
|
|
|
reduce: Function
|
|
|
|
});
|
|
|
|
|
2016-11-17 19:54:48 +02:00
|
|
|
_.extend(this[storage], {
|
|
|
|
[reducerName]: reducerConfig
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param name
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
getReducer(name) {
|
|
|
|
if (this[storage]) {
|
|
|
|
return this[storage][name];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|