Completed
Push — master ( 1719ac...509a65 )
by Jeroen De
10s
created

lidationDispatcherCollection   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 9
rs 9.6666
c 1
b 0
f 0
1
'use strict';
2
3
/**
4
 *
5
 * @module redux_validation
6
 */
7
8
var objectAssign = require( 'object-assign' ),
9
10
	ValidationDispatcherCollection = {
11
		dispatchers: [],
12
		store: null,
13
		formContentName: '',
14
		onUpdate: function () {
15
			var formContent = this.store.getState()[ this.formContentName ],
16
				i;
17
			for ( i = 0; i < this.dispatchers.length; i++ ) {
18
				this.dispatchers[ i ].dispatchIfChanged( formContent, this.store );
19
			}
20
		}
21
	},
22
23
	/**
24
	 *
25
	 * @param {Object} store Redux store
26
	 * @param {ValidationDispatcher[]} dispatchers
27
	 * @param {string} formContentName Field name for the store to access form contents, e.g. 'donationFormContent' or 'membershipFormContent'
28
	 */
29
	createValidationDispatcherCollection = function ( store, dispatchers, formContentName ) {
30
		var collection = objectAssign( Object.create( ValidationDispatcherCollection ), {
31
			store: store,
32
			dispatchers: dispatchers,
33
			formContentName: formContentName
34
		} );
35
		store.subscribe( collection.onUpdate.bind( collection ) );
36
		return collection;
37
	};
38
39
module.exports = {
40
	createValidationDispatcherCollection: createValidationDispatcherCollection
41
};
42