Skip to content

Commit 329b51e

Browse files
committed
Move form lookup to controller
Using angular.element.data doesn't work with arrays inside ng-repeat
1 parent 9a8da19 commit 329b51e

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
lines changed

src/directives/field.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ angular.module('schemaForm').directive('sfField',
99
replace: false,
1010
transclude: false,
1111
scope: true,
12-
require: '?^sfSchema',
12+
require: '^sfSchema',
1313
link: {
14-
pre: function(scope, element, attrs) {
14+
pre: function(scope, element, attrs, sfSchema) {
1515
//The ngModelController is used in some templates and
1616
//is needed for error messages,
1717
scope.$on('schemaFormPropagateNgModelController', function(event, ngModel) {
@@ -21,7 +21,7 @@ angular.module('schemaForm').directive('sfField',
2121
});
2222

2323
// Fetch our form.
24-
scope.form = element.data('sfForm');
24+
scope.form = sfSchema.lookup['f' + attrs.sfField];
2525
},
2626
post: function(scope, element, attrs, sfSchema) {
2727
//Keep error prone logic from the template

src/directives/schema-form.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ angular.module('schemaForm')
1919
this.evalInParentScope = function(expr, locals) {
2020
return $scope.$parent.$eval(expr, locals);
2121
};
22+
23+
// Set up form lookup map
24+
var that = this;
25+
$scope.lookup = function(lookup) {
26+
if (lookup) {
27+
that.lookup = lookup;
28+
}
29+
return that.lookup;
30+
};
2231
}],
2332
replace: false,
2433
restrict: 'A',
@@ -86,13 +95,13 @@ angular.module('schemaForm')
8695

8796
// if sfUseDecorator is undefined the default decorator is used.
8897
var decorator = schemaFormDecorators.decorator(attrs.sfUseDecorator);
89-
9098
// Use the builder to build it and append the result
91-
element[0].appendChild( sfBuilder.build(merged, decorator, slots) );
92-
99+
var lookup = Object.create(null);
100+
scope.lookup(lookup); // give the new lookup to the controller.
101+
element[0].appendChild(sfBuilder.build(merged, decorator, slots, lookup));
93102
//compile only children
94103
$compile(element.children())(childScope);
95-
104+
96105
//ok, now that that is done let's set any defaults
97106
if (!scope.options || scope.options.setSchemaDefaults !== false) {
98107
schemaForm.traverseSchema(schema, function(prop, path) {
@@ -106,6 +115,7 @@ angular.module('schemaForm')
106115
}
107116

108117
scope.$emit('sf-render-finished', element);
118+
console.timeEnd('render')
109119
};
110120

111121
var defaultForm = ['*'];

src/services/builder.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@ angular.module('schemaForm').provider('sfBuilder', ['sfPathProvider', function(s
99
return (pos ? separator : '') + letter.toLowerCase();
1010
});
1111
};
12+
var formId = 0;
1213

1314
var builders = {
1415
sfField: function(args) {
15-
args.fieldFrag.firstChild.setAttribute('sf-field', args.path);
16-
angular.element.data(args.fieldFrag.firstChild, 'sfForm', args.form);
16+
args.fieldFrag.firstChild.setAttribute('sf-field', formId);
17+
18+
// We use a lookup table for easy access to our form.
19+
args.lookup['f' + formId] = args.form;
20+
formId++;
1721
},
1822
ngModel: function(args) {
1923
if (!args.form.key) {
@@ -117,8 +121,9 @@ angular.module('schemaForm').provider('sfBuilder', ['sfPathProvider', function(s
117121
}
118122
};
119123

120-
var build = function(items, decorator, templateFn, slots, path, state) {
124+
var build = function(items, decorator, templateFn, slots, path, state, lookup) {
121125
state = state || {};
126+
lookup = lookup || Object.create(null);
122127
path = path || 'schemaForm.form';
123128
var container = document.createDocumentFragment();
124129
items.reduce(function(frag, f, index) {
@@ -155,12 +160,13 @@ angular.module('schemaForm').provider('sfBuilder', ['sfPathProvider', function(s
155160
var args = {
156161
fieldFrag: tmpl,
157162
form: f,
163+
lookup: lookup,
158164
state: state,
159165
path: path + '[' + index + ']',
160166

161167
// Recursive build fn
162168
build: function(items, path, state) {
163-
return build(items, decorator, templateFn, slots, path, state);
169+
return build(items, decorator, templateFn, slots, path, state, lookup);
164170
},
165171

166172
};
@@ -185,10 +191,10 @@ angular.module('schemaForm').provider('sfBuilder', ['sfPathProvider', function(s
185191
/**
186192
* Builds a form from a canonical form definition
187193
*/
188-
build: function(form, decorator, slots) {
194+
build: function(form, decorator, slots, lookup) {
189195
return build(form, decorator, function(url) {
190196
return $templateCache.get(url);
191-
}, slots);
197+
}, slots, undefined, undefined, lookup);
192198

193199
},
194200
builder: builders,

0 commit comments

Comments
 (0)