Skip to content

Commit e8d22c0

Browse files
committed
Moved destroy handling to decorator directive
Also added one basic test. Note to self: add more tests.
1 parent a9fbd9b commit e8d22c0

File tree

6 files changed

+298
-239
lines changed

6 files changed

+298
-239
lines changed

src/directives/schema-form.js

+2-5
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', 'sfRetainModel',
9-
function($compile, schemaForm, schemaFormDecorators, sfSelect, sfPath, sfRetainModel) {
8+
['$compile', 'schemaForm', 'schemaFormDecorators', 'sfSelect', 'sfPath',
9+
function($compile, schemaForm, schemaFormDecorators, sfSelect, sfPath) {
1010

1111
var SNAKE_CASE_REGEXP = /[A-Z]/g;
1212
var snakeCase = function(name, separator) {
@@ -165,16 +165,13 @@ angular.module('schemaForm')
165165
});
166166

167167
scope.$on('$destroy', function() {
168-
console.log('Total schema form destruction in progress.');
169168
// Each field listens to the $destroy event so that it can remove any value
170169
// from the model if that field is removed from the form. This is the default
171170
// destroy strategy. But if the entire form (or at least the part we're on)
172171
// gets removed, like when routing away to another page, then we definetly want to
173172
// keep the model intact. So therefore we set a flag to tell the others it's time to just
174173
// let it be.
175174
scope.externalDestructionInProgress = true;
176-
//scope.$broadcast('externalDestroy')
177-
//sfRetainModel.setFlag(true);
178175
});
179176
}
180177
};

src/directives/schema-validate.js

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

44
return {
55
restrict: 'A',
@@ -102,116 +102,6 @@ angular.module('schemaForm').directive('schemaValidate', ['sfValidator', 'sfSele
102102

103103
});
104104

105-
106-
/*var DEFAULT_DESTROY_STRATEGY = getGlobalOptionsDestroyStrategy();
107-
108-
function getGlobalOptionsDestroyStrategy() {
109-
var defaultStrategy = undefined;
110-
if (scope.options && scope.options.hasOwnProperty('destroyStrategy')) {
111-
var globalOptionsDestroyStrategy = scope.options.destroyStrategy;
112-
var isValidFormDefaultDestroyStrategy = (globalOptionsDestroyStrategy === undefined ||
113-
globalOptionsDestroyStrategy === '' ||
114-
globalOptionsDestroyStrategy === null ||
115-
globalOptionsDestroyStrategy === 'retain');
116-
if (isValidFormDefaultDestroyStrategy) {
117-
defaultStrategy = globalOptionsDestroyStrategy;
118-
}
119-
else {
120-
console.warn('Unrecognized globalOptions destroyStrategy: %s \'%s\'. Used undefined instead.',
121-
typeof globalOptionsDestroyStrategy, globalOptionsDestroyStrategy);
122-
}
123-
}
124-
return defaultStrategy;
125-
}*/
126-
127-
128-
// Clean up the model when the corresponding form field is $destroy-ed.
129-
// Default behavior can be supplied as a globalOption, and behavior can be overridden in the form definition.
130-
scope.$on('$destroy', function() {
131-
console.log('Schema validate destroy', scope.externalDestructionInProgress);
132-
133-
// If the entire schema form is destroyed we don't touch the model
134-
if (!scope.externalDestructionInProgress) {
135-
var form = getForm();
136-
var destroyStrategy = form.destroyStrategy ||
137-
(scope.options && scope.options.destroyStrategy) || 'remove';
138-
console.log('going with destroy strategy ', destroyStrategy, scope.options)
139-
// No key no model, and we might have strategy 'retain'
140-
if (form.key && destroyStrategy !== 'retain') {
141-
142-
// Get the object that has the property we wan't to clear.
143-
var obj = scope.model;
144-
if (form.key.length > 1) {
145-
obj = sfSelect(form.key.slice(0, form.key.length - 1), obj);
146-
}
147-
148-
// We can get undefined here if the form hasn't been filled out entirely
149-
if (obj === undefined) {
150-
return;
151-
}
152-
153-
// Type can also be a list in JSON Schema
154-
var type = (form.schema && form.schema.type) || '';
155-
156-
// Empty means '',{} and [] for appropriate types and undefined for the rest
157-
console.log('destroy', destroyStrategy, form.key, type, obj);
158-
if (destroyStrategy === 'empty' && type.indexOf('string') !== -1) {
159-
obj[form.key.slice(-1)] = '';
160-
} else if (destroyStrategy === 'empty' && type.indexOf('object') !== -1) {
161-
obj[form.key.slice(-1)] = {};
162-
} else if (destroyStrategy === 'empty' && type.indexOf('array') !== -1) {
163-
obj[form.key.slice(-1)] = [];
164-
} else if (destroyStrategy === 'null') {
165-
obj[form.key.slice(-1)] = null;
166-
} else {
167-
delete obj[form.key.slice(-1)];
168-
}
169-
}
170-
}
171-
172-
173-
/*var conditionResult = $parse(form.condition);
174-
var formModelNotRetained = !sfRetainModel.getFlag();
175-
176-
// If condition is defined and not satisfied and the sfSchema model should not be retained.
177-
if (form.hasOwnProperty('condition') && !conditionResult(scope) && formModelNotRetained) {
178-
179-
// Either set in form definition, or as part of globalOptions.
180-
var destroyStrategy =
181-
!form.hasOwnProperty('destroyStrategy') ? DEFAULT_DESTROY_STRATEGY : form.destroyStrategy;
182-
var schemaType = getSchemaType();
183-
184-
if (destroyStrategy && destroyStrategy !== 'retain') {
185-
// Don't recognize the strategy, so give a warning.
186-
console.warn('%s has defined unrecognized destroyStrategy: \'%s\'. Used default instead.',
187-
attrs.name, destroyStrategy);
188-
destroyStrategy = DEFAULT_DESTROY_STRATEGY;
189-
}
190-
else if (schemaType !== 'string' && destroyStrategy === '') {
191-
// Only 'string' type fields can have an empty string value as a valid option.
192-
console.warn('%s attempted to use empty string destroyStrategy on non-string form type. ' +
193-
'Used default instead.', attrs.name);
194-
destroyStrategy = DEFAULT_DESTROY_STRATEGY;
195-
}
196-
197-
if (destroyStrategy === 'retain') {
198-
return; // Valid option to avoid destroying data in the model.
199-
}
200-
201-
destroyUsingStrategy(destroyStrategy);
202-
203-
function destroyUsingStrategy(strategy) {
204-
var strategyIsDefined = (strategy === null || strategy === '' || strategy === undefined);
205-
if (!strategyIsDefined) {
206-
strategy = DEFAULT_DESTROY_STRATEGY;
207-
}
208-
sfUnselect(scope.form.key, scope.model, strategy);
209-
}
210-
}
211-
*/
212-
});
213-
214-
215105
scope.schemaError = function() {
216106
return error;
217107
};

src/services/decorators.js

+46-3
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ angular.module('schemaForm').provider('schemaFormDecorators',
3131

3232
var createDirective = function(name) {
3333
$compileProvider.directive(name,
34-
['$parse', '$compile', '$http', '$templateCache', '$interpolate', '$q', 'sfErrorMessage', 'sfPath',
35-
function($parse, $compile, $http, $templateCache, $interpolate, $q, sfErrorMessage, sfPath) {
34+
['$parse', '$compile', '$http', '$templateCache', '$interpolate', '$q', 'sfErrorMessage',
35+
'sfPath','sfSelect',
36+
function($parse, $compile, $http, $templateCache, $interpolate, $q, sfErrorMessage,
37+
sfPath, sfSelect) {
3638

3739
return {
3840
restrict: 'AE',
@@ -261,7 +263,48 @@ angular.module('schemaForm').provider('schemaFormDecorators',
261263
scope.$broadcast('schemaFormValidate');
262264
}
263265
}
264-
})
266+
});
267+
268+
// Clean up the model when the corresponding form field is $destroy-ed.
269+
// Default behavior can be supplied as a globalOption, and behavior can be overridden in the form definition.
270+
scope.$on('$destroy', function() {
271+
// If the entire schema form is destroyed we don't touch the model
272+
if (!scope.externalDestructionInProgress) {
273+
var destroyStrategy = form.destroyStrategy ||
274+
(scope.options && scope.options.destroyStrategy) || 'remove';
275+
// No key no model, and we might have strategy 'retain'
276+
if (form.key && destroyStrategy !== 'retain') {
277+
278+
// Get the object that has the property we wan't to clear.
279+
var obj = scope.model;
280+
if (form.key.length > 1) {
281+
obj = sfSelect(form.key.slice(0, form.key.length - 1), obj);
282+
}
283+
284+
// We can get undefined here if the form hasn't been filled out entirely
285+
if (obj === undefined) {
286+
return;
287+
}
288+
289+
// Type can also be a list in JSON Schema
290+
var type = (form.schema && form.schema.type) || '';
291+
292+
// Empty means '',{} and [] for appropriate types and undefined for the rest
293+
//console.log('destroy', destroyStrategy, form.key, type, obj);
294+
if (destroyStrategy === 'empty' && type.indexOf('string') !== -1) {
295+
obj[form.key.slice(-1)] = '';
296+
} else if (destroyStrategy === 'empty' && type.indexOf('object') !== -1) {
297+
obj[form.key.slice(-1)] = {};
298+
} else if (destroyStrategy === 'empty' && type.indexOf('array') !== -1) {
299+
obj[form.key.slice(-1)] = [];
300+
} else if (destroyStrategy === 'null') {
301+
obj[form.key.slice(-1)] = null;
302+
} else {
303+
delete obj[form.key.slice(-1)];
304+
}
305+
}
306+
}
307+
});
265308
}
266309

267310
once();

src/services/retainModel.js

-37
This file was deleted.

src/services/unselect.js

-82
This file was deleted.

0 commit comments

Comments
 (0)