cellFieldCollection.js
2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* Holds all of our cell field models.
*
* @package Ninja Forms Layouts
* @subpackage Fields
* @copyright (c) 2016 WP Ninjas
* @since 3.0
*/
define( [], function( ) {
var collection = Backbone.Collection.extend( {
comparator: 'order',
initialize: function( models, options ) {
// We've been passed the cellModel to which this collection belongs.
this.cellModel = options.cellModel;
_.each( models, function( model ) {
model.set( 'cellcid', this.cellModel.cid, { silent: true } );
}, this );
this.listenTo( this.cellModel.collection.rowModel.collection, 'validate:fields', this.validateFields );
this.listenTo( this.cellModel.collection.rowModel.collection, 'show:fields', this.showFields );
this.listenTo( this.cellModel.collection.rowModel.collection, 'hide:fields', this.hideFields );
var fieldCollection = this.cellModel.collection.formModel.get( 'fields' );
// When we remove a model from our main field collection, make sure it's removed from this collection as well.
fieldCollection.on( 'reset', this.resetCollection, this );
},
validateFields: function() {
_.each( this.models, function( fieldModel ) {
fieldModel.set( 'clean', false ); // @todo remove after this is released in core.
nfRadio.channel( 'submit' ).trigger( 'validate:field', fieldModel );
}, this );
},
showFields: function() {
this.invoke( 'set', { visible: true } );
this.invoke( function() {
this.trigger( 'change:value', this );
});
},
hideFields: function() {
this.invoke( 'set', { visible: false } );
this.invoke( function() {
this.trigger( 'change:value', this );
});
},
/**
* When we reset our main field collection, we need to reset any of those fields in this collection.
*
* @since 3.0.12
* @param Backbone.Collection collection
* @return void
*/
resetCollection: function( collection ) {
var fieldModels = [];
_.each( this.models, function( fieldModel ) {
if ( 'submit' != fieldModel.get( 'type' ) ) {
fieldModels.push( collection.findWhere( { key: fieldModel.get( 'key' ) } ) );
} else {
fieldModels.push( fieldModel );
}
} );
this.reset( fieldModels );
}
} );
return collection;
} );