Skip to content
This repository has been archived by the owner on Apr 30, 2018. It is now read-only.

Commit

Permalink
adds manualModelWatcher option
Browse files Browse the repository at this point in the history
  • Loading branch information
Wypchlo, Karol (Consultant) authored and Wypchlo, Karol (Consultant) committed Jan 8, 2016
1 parent 5dbfbb8 commit 94215a3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 16 deletions.
60 changes: 44 additions & 16 deletions src/directives/formly-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,28 +112,56 @@ function formlyForm(formlyUsability, formlyWarn, $parse, formlyConfig, $interpol
setupFields()

// watch the model and evaluate watch expressions that depend on it.
$scope.$watch('model', onModelOrFormStateChange, true)
if ($scope.options.manualModelWatcher) {
let stringWatchers = {}

angular.forEach($scope.fields, function(field, index) {
if (field.data && Array.isArray(field.data.watch)) {
field.data.watch.forEach(function(expression) {
if (typeof expression === 'string') {
stringWatchers[expression] = stringWatchers[expression] || []
stringWatchers[expression].push(runFieldExpressionProperties.bind(null, field, index))
} else {
$scope.$watch(expression, function manualFieldModelWatcher() {
runFieldExpressionProperties(field, index)
}, true)
}
})
}
})

for (let expression in stringWatchers) {
$scope.$watch(expression, function manualFieldModelWatcher() {
stringWatchers[expression].forEach(stringWatchers[expression])
}, true)
}
} else {
$scope.$watch('model', onModelOrFormStateChange, true)
}

if ($scope.options.formState) {
$scope.$watch('options.formState', onModelOrFormStateChange, true)
}

function onModelOrFormStateChange() {
angular.forEach($scope.fields, function runFieldExpressionProperties(field, index) {
const model = field.model || $scope.model
const promise = field.runExpressions && field.runExpressions()
if (field.hideExpression) { // can't use hide with expressionProperties reliably
const val = model[field.key]
field.hide = evalCloseToFormlyExpression(field.hideExpression, val, field, index)
}
if (field.extras && field.extras.validateOnModelChange && field.formControl) {
const validate = field.formControl.$validate
if (promise) {
promise.then(validate)
} else {
validate()
}
angular.forEach($scope.fields, runFieldExpressionProperties)
}

function runFieldExpressionProperties(field, index) {
const model = field.model || $scope.model
const promise = field.runExpressions && field.runExpressions()
if (field.hideExpression) { // can't use hide with expressionProperties reliably
const val = model[field.key]
field.hide = evalCloseToFormlyExpression(field.hideExpression, val, field, index)
}
if (field.extras && field.extras.validateOnModelChange && field.formControl) {
const validate = field.formControl.$validate
if (promise) {
promise.then(validate)
} else {
validate()
}
})
}
}

function setupFields() {
Expand Down
1 change: 1 addition & 0 deletions src/providers/formlyApiCheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ const formOptionsApi = apiCheck.shape({
updateInitialValue: apiCheck.func.optional,
removeChromeAutoComplete: apiCheck.bool.optional,
templateManipulators: templateManipulators.optional,
manualModelWatcher: apiCheck.object.optional,
wrapper: specifyWrapperType.optional,
fieldTransform: apiCheck.oneOfType([
apiCheck.func, apiCheck.array,
Expand Down

0 comments on commit 94215a3

Please sign in to comment.