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

app/js/lib/validation_dispatcher_collection.js   A

Complexity

Total Complexity 3
Complexity/F 1.5

Size

Lines of Code 41
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
nc 1
dl 0
loc 41
rs 10
c 1
b 0
f 0
wmc 3
mnd 1
bc 3
fnc 2
bpm 1.5
cpm 1.5
noi 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A validation_dispatcher_collection.js ➔ createValidationDispatcherCollection 0 9 1
A ValidationDispatcherCollection.onUpdate 0 7 2
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