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

refactor(form): remove the use of the private setter function #12483

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions src/.jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,6 @@
/* ng/compile.js */
"directiveNormalize": false,

/* ng/parse.js */
"setter": false,

/* ng/directive/directives.js */
"ngDirective": false,

Expand Down
21 changes: 14 additions & 7 deletions src/ng/directive/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ function FormController(element, attrs, $scope, $animate, $interpolate) {
* related scope, under this name.
*/
var formDirectiveFactory = function(isNgForm) {
return ['$timeout', function($timeout) {
return ['$timeout', '$parse', function($timeout, $parse) {
var formDirective = {
name: 'form',
restrict: isNgForm ? 'EAC' : 'E',
Expand Down Expand Up @@ -491,21 +491,21 @@ var formDirectiveFactory = function(isNgForm) {
}

var parentFormCtrl = controller.$$parentForm;
var setter = nameAttr ? getSetter(controller.$name) : noop;

if (nameAttr) {
setter(scope, controller.$name, controller, controller.$name);
setter(scope, controller);
attr.$observe(nameAttr, function(newValue) {
if (controller.$name === newValue) return;
setter(scope, controller.$name, undefined, controller.$name);
setter(scope, undefined);
parentFormCtrl.$$renameControl(controller, newValue);
setter(scope, controller.$name, controller, controller.$name);
setter = getSetter(controller.$name);
setter(scope, controller);
});
}
formElement.on('$destroy', function() {
parentFormCtrl.$removeControl(controller);
if (nameAttr) {
setter(scope, attr[nameAttr], undefined, controller.$name);
}
setter(scope, undefined);
extend(controller, nullFormCtrl); //stop propagating child destruction handlers upwards
});
}
Expand All @@ -514,6 +514,13 @@ var formDirectiveFactory = function(isNgForm) {
};

return formDirective;

function getSetter(expression) {
if (expression === '') {
return $parse('this[""]').assign;
Copy link
Contributor

Choose a reason for hiding this comment

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

What exactly does this do?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

410f7c6 added some code changes and a test so when the name is the empty string, then the form object is stored in the scope under the '' name. This code workarounds this case as the empty expression is not assignable

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks!

}
return $parse(expression).assign || noop;
}
}];
};

Expand Down
23 changes: 0 additions & 23 deletions src/ng/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -1610,29 +1610,6 @@ Parser.prototype = {
}
};

//////////////////////////////////////////////////
// Parser helper functions
//////////////////////////////////////////////////

function setter(obj, path, setValue, fullExp) {
ensureSafeObject(obj, fullExp);

var element = path.split('.'), key;
for (var i = 0; element.length > 1; i++) {
key = ensureSafeMemberName(element.shift(), fullExp);
var propertyObj = ensureSafeObject(obj[key], fullExp);
if (!propertyObj) {
propertyObj = {};
obj[key] = propertyObj;
}
obj = propertyObj;
}
key = ensureSafeMemberName(element.shift(), fullExp);
ensureSafeObject(obj[key], fullExp);
obj[key] = setValue;
return setValue;
}

var getterFnCacheDefault = createMap();
var getterFnCacheExpensive = createMap();

Expand Down