Skip to content
Merged
Prev Previous commit
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.
  • Loading branch information
jbsaff committed May 4, 2015
commit dcff62b5cdf71d16076cddef0b95523d2c20c74d
7 changes: 5 additions & 2 deletions src/directives/schema-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ FIXME: real documentation

angular.module('schemaForm')
.directive('sfSchema',
['$compile', 'schemaForm', 'schemaFormDecorators', 'sfSelect', 'sfPath',
function($compile, schemaForm, schemaFormDecorators, sfSelect, sfPath) {
['$compile', 'schemaForm', 'schemaFormDecorators', 'sfSelect', 'sfPath', 'sfRetainModel',
function($compile, schemaForm, schemaFormDecorators, sfSelect, sfPath, sfRetainModel) {

var SNAKE_CASE_REGEXP = /[A-Z]/g;
var snakeCase = function(name, separator) {
Expand Down Expand Up @@ -161,6 +161,9 @@ angular.module('schemaForm')
}
});

scope.$on('$destroy', function() {
sfRetainModel.setFlag(true);
});
}
};
}
Expand Down
9 changes: 6 additions & 3 deletions src/directives/schema-validate.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
angular.module('schemaForm').directive('schemaValidate', ['sfValidator', 'sfSelect', 'sfUnselect', '$parse',
function(sfValidator, sfSelect, sfUnselect, $parse) {
angular.module('schemaForm').directive('schemaValidate', ['sfValidator', 'sfSelect', 'sfUnselect', '$parse', 'sfRetainModel',
function(sfValidator, sfSelect, sfUnselect, $parse, sfRetainModel) {

return {
restrict: 'A',
Expand Down Expand Up @@ -127,10 +127,13 @@ angular.module('schemaForm').directive('schemaValidate', ['sfValidator', 'sfSele
// Clean up the model when the corresponding form field is $destroy-ed.
// Default behavior can be supplied as a globalOption, and behavior can be overridden in the form definition.
scope.$on('$destroy', function() {

var form = getForm();
var conditionResult = $parse(form.condition);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was looking through the code and testing some, and I'm not quite sure why we need to check on the condition here? Shouldn't we always just follow the destroy strategy if we get a destroy event? I'm thinking that condition might not be the only thing that can remove a form element from the form, any other add on might use a ng-if somewhere and since any field not in the DOM won't get any validation done on it anyways It's probably best to remove it from the model.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had left it in as a way to help show where I was coming from with my solution, because I wasn't 100% confident that the service I put in would be correct (I'd still label myself an apprentice with Javascript). Assuming that the flag will handle for the case where the destroy is coming from outside of the form, then I agree that destroys initiated within the form should just follow the destroyStrategy. It'll be more consistent (and still configurable) and easier to explain.

var formModelNotRetained = !sfRetainModel.getFlag();

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

// Either set in form definition, or as part of globalOptions.
var destroyStrategy =
Expand Down
37 changes: 37 additions & 0 deletions src/services/retainModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
angular.module('schemaForm').factory('sfRetainModel', function() {

var data = {retainModelFlag: false };

return {
/**
* @description
* Utility service to indicate if the sfSchema model should be retained.
* Set to true to prevent an operation that would have destroyed the model
* from doing so (such as wrapping the form in an ng-if).
*
* ex.
* var foo = sfRetainModel.getFlag();
*
* @returns {boolean} returns the current value of the retainModelFlag.
*/
getFlag: function () {
return data.retainModelFlag;
},

/**
* @description
* Set the value of the retainModelFlag.
* True prevents cleaning the data in the model, while false follows the configured destroyStrategy.
*
* ex.
* var bar = sfRetainModel.setFlag(true);
*
* @param {boolean} value The boolean value to set as the retainModelFlag
* @returns {boolean} returns the value of the retainModelFlag after toggling.
*/
setFlag: function(value) {
data.retainModelFlag = value;
return data.retainModelFlag;
}
}
});