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

Commit 33ae9c6

Browse files
author
Kent C. Dodds
committed
feat(ngModelAttrsManipulator): 🎆 Add support for property accessor keys 🎇
This simply replaces the with the given key and is prefixed with . Unfortunately, this current approach wont work for braket syntax for the first property, but should cover most use cases closes #452 BREAKING CHANGE: We're now altering the ng-model regardless of anything. If you used to have dots or brackets in your properties, this will break you. Most of you probably wont have a problem.
1 parent 8ee48ac commit 33ae9c6

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

‎src/other/utils.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import angular from 'angular-fix';
22

3-
export default {formlyEval, getFieldId, reverseDeepMerge, findByNodeName, arrayify, extendFunction, extendArray, startsWith};
3+
export default {
4+
formlyEval, getFieldId, reverseDeepMerge, findByNodeName, arrayify, extendFunction, extendArray, startsWith, contains
5+
};
46

57
function formlyEval(scope, expression, $modelValue, $viewValue, extraLocals) {
68
if (angular.isFunction(expression)) {
@@ -109,3 +111,11 @@ function startsWith(str, search) {
109111
return false;
110112
}
111113
}
114+
115+
function contains(str, search) {
116+
if (angular.isString(str) && angular.isString(search)) {
117+
return str.length >= search.length && str.indexOf(search) !== -1;
118+
} else {
119+
return false;
120+
}
121+
}

‎src/run/formlyNgModelAttrsManipulator.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import angular from 'angular-fix';
2+
import {contains} from '../other/utils';
23

34
export default addFormlyNgModelAttrsManipulator;
45

@@ -27,6 +28,7 @@ function addFormlyNgModelAttrsManipulator(formlyConfig, $interpolate, formlyWarn
2728
addIfNotPresent(modelNodes, 'name', scope.name || scope.id);
2829

2930
addValidation();
31+
alterNgModelAttr();
3032
addModelOptions();
3133
addTemplateOptionsAttrs();
3234
addNgModelElAttrs();
@@ -41,13 +43,17 @@ function addFormlyNgModelAttrsManipulator(formlyConfig, $interpolate, formlyWarn
4143
}
4244
}
4345

46+
function alterNgModelAttr() {
47+
if (isPropertyAccessor(options.key)) {
48+
addRegardlessOfPresence(modelNodes, 'ng-model', 'model.' + options.key);
49+
}
50+
}
51+
4452
function addModelOptions() {
4553
if (angular.isDefined(options.modelOptions)) {
4654
addIfNotPresent(modelNodes, 'ng-model-options', 'options.modelOptions');
4755
if (options.modelOptions.getterSetter) {
48-
angular.forEach(modelNodes, modelNode => {
49-
modelNode.setAttribute('ng-model', 'options.value');
50-
});
56+
addRegardlessOfPresence(modelNodes, 'ng-model', 'options.value');
5157
}
5258
}
5359
}
@@ -235,4 +241,14 @@ function addFormlyNgModelAttrsManipulator(formlyConfig, $interpolate, formlyWarn
235241
}
236242
});
237243
}
244+
245+
function addRegardlessOfPresence(nodes, attr, val) {
246+
angular.forEach(nodes, node => {
247+
node.setAttribute(attr, val);
248+
});
249+
}
250+
251+
function isPropertyAccessor(key) {
252+
return contains(key, '.') || (contains(key, '[') && contains(key, ']'));
253+
}
238254
}

‎src/run/formlyNgModelAttrsManipulator.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,19 @@ describe('formlyNgModelAttrsManipulator', () => {
194194
});
195195
});
196196

197+
describe(`selector key notation`, () => {
198+
it(`should change the ng-model when the key is a dot property accessor`, () => {
199+
field.key = 'bar.foo';
200+
manipulate();
201+
expect(resultEl.attr('ng-model')).to.equal('model.' + field.key);
202+
});
203+
204+
it(`should change the ng-model when the key is a bracket property accessor`, () => {
205+
field.key = 'bar["foo-bar"]';
206+
manipulate();
207+
expect(resultEl.attr('ng-model')).to.equal('model.' + field.key);
208+
});
209+
});
197210

198211
describe(`formly-custom-validation`, () => {
199212
it(`shouldn't be added if there aren't validators or messages`, () => {

0 commit comments

Comments
 (0)