Skip to content

Commit dcff62b

Browse files
committed
sfSchema can communicate via sfRetainModel service to force the model to ignore the destroyStrategy when the $destroy event occurs on sfSchema. This will prevent the data from being cleaned up when the form is removed from the DOM.
1 parent d20b273 commit dcff62b

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

src/directives/schema-form.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ FIXME: real documentation
55

66
angular.module('schemaForm')
77
.directive('sfSchema',
8-
['$compile', 'schemaForm', 'schemaFormDecorators', 'sfSelect', 'sfPath',
9-
function($compile, schemaForm, schemaFormDecorators, sfSelect, sfPath) {
8+
['$compile', 'schemaForm', 'schemaFormDecorators', 'sfSelect', 'sfPath', 'sfRetainModel',
9+
function($compile, schemaForm, schemaFormDecorators, sfSelect, sfPath, sfRetainModel) {
1010

1111
var SNAKE_CASE_REGEXP = /[A-Z]/g;
1212
var snakeCase = function(name, separator) {
@@ -161,6 +161,9 @@ angular.module('schemaForm')
161161
}
162162
});
163163

164+
scope.$on('$destroy', function() {
165+
sfRetainModel.setFlag(true);
166+
});
164167
}
165168
};
166169
}

src/directives/schema-validate.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
angular.module('schemaForm').directive('schemaValidate', ['sfValidator', 'sfSelect', 'sfUnselect', '$parse',
2-
function(sfValidator, sfSelect, sfUnselect, $parse) {
1+
angular.module('schemaForm').directive('schemaValidate', ['sfValidator', 'sfSelect', 'sfUnselect', '$parse', 'sfRetainModel',
2+
function(sfValidator, sfSelect, sfUnselect, $parse, sfRetainModel) {
33

44
return {
55
restrict: 'A',
@@ -127,10 +127,13 @@ angular.module('schemaForm').directive('schemaValidate', ['sfValidator', 'sfSele
127127
// Clean up the model when the corresponding form field is $destroy-ed.
128128
// Default behavior can be supplied as a globalOption, and behavior can be overridden in the form definition.
129129
scope.$on('$destroy', function() {
130+
130131
var form = getForm();
131132
var conditionResult = $parse(form.condition);
133+
var formModelNotRetained = !sfRetainModel.getFlag();
132134

133-
if (form.hasOwnProperty('condition') && !conditionResult(scope)) { // If condition is defined and not satisfied.
135+
// If condition is defined and not satisfied and the sfSchema model should not be retained.
136+
if (form.hasOwnProperty('condition') && !conditionResult(scope) && formModelNotRetained) {
134137

135138
// Either set in form definition, or as part of globalOptions.
136139
var destroyStrategy =

src/services/retainModel.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
angular.module('schemaForm').factory('sfRetainModel', function() {
2+
3+
var data = {retainModelFlag: false };
4+
5+
return {
6+
/**
7+
* @description
8+
* Utility service to indicate if the sfSchema model should be retained.
9+
* Set to true to prevent an operation that would have destroyed the model
10+
* from doing so (such as wrapping the form in an ng-if).
11+
*
12+
* ex.
13+
* var foo = sfRetainModel.getFlag();
14+
*
15+
* @returns {boolean} returns the current value of the retainModelFlag.
16+
*/
17+
getFlag: function () {
18+
return data.retainModelFlag;
19+
},
20+
21+
/**
22+
* @description
23+
* Set the value of the retainModelFlag.
24+
* True prevents cleaning the data in the model, while false follows the configured destroyStrategy.
25+
*
26+
* ex.
27+
* var bar = sfRetainModel.setFlag(true);
28+
*
29+
* @param {boolean} value The boolean value to set as the retainModelFlag
30+
* @returns {boolean} returns the value of the retainModelFlag after toggling.
31+
*/
32+
setFlag: function(value) {
33+
data.retainModelFlag = value;
34+
return data.retainModelFlag;
35+
}
36+
}
37+
});

0 commit comments

Comments
 (0)