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

Commit 9b4e0c9

Browse files
tboschjeffbcross
authored andcommitted
wip: simplify select
1 parent 67115f3 commit 9b4e0c9

File tree

1 file changed

+23
-63
lines changed

1 file changed

+23
-63
lines changed

src/ng/directive/select.js

+23-63
Original file line numberDiff line numberDiff line change
@@ -369,25 +369,17 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
369369
collection = valuesFn(scope) || [],
370370
locals = {},
371371
key, value, optionElement, index, groupIndex, length, groupLength, trackIndex;
372-
372+
var selectedKeys = selectElement.val();
373+
var viewValue;
373374
if (multiple) {
374-
value = [];
375-
for (groupIndex = 0, groupLength = optionGroupsCache.length;
376-
groupIndex < groupLength;
377-
groupIndex++) {
378-
// list of options for that group. (first item has the parent)
379-
optionGroup = optionGroupsCache[groupIndex];
380-
381-
for(index = 1, length = optionGroup.length; index < length; index++) {
382-
if ((optionElement = optionGroup[index].element)[0].selected) {
383-
value.push(getViewValue(optionElement, locals, collection));
384-
}
385-
}
386-
}
375+
viewValue = [];
376+
forEach(selectedKeys, function(selectedKey) {
377+
viewValue.push(getViewValue(selectedKey, collection));
378+
});
387379
} else {
388-
value = getViewValue(selectElement, locals, collection);
380+
viewValue = getViewValue(selectedKeys, collection);
389381
}
390-
ctrl.$setViewValue(value);
382+
ctrl.$setViewValue(viewValue);
391383
render();
392384
});
393385
});
@@ -431,56 +423,24 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
431423
return selectedSet;
432424
}
433425

434-
function getViewValue (el, locals, collection) {
435-
var key = el.val();
436-
var value;
437-
var calculateViewValue;
438-
439-
if (selectAsFn || trackFn) {
440-
calculateViewValue = function () {
441-
var getterFn = selectAsFn || trackFn;
442-
var wrappedCollectionValue = {};
443-
wrappedCollectionValue[valueName] = collection[key];
444-
wrappedCollectionValue[keyName] = key;
445-
446-
for (var i in collection) {
447-
if (collection.hasOwnProperty(i)) {
448-
locals[valueName] = collection[i];
449-
if (keyName) locals[keyName] = i;
450-
if (getterFn(scope, locals) ==
451-
getterFn(scope, wrappedCollectionValue)) {
452-
/*
453-
* trackBy should not be used for final calculation, because it doesn't
454-
* necessarily return the expected value.
455-
*/
456-
return (selectAsFn||valueFn)(scope, locals);
457-
}
458-
}
459-
}
460-
};
461-
} else {
462-
calculateViewValue = function() {
463-
locals[valueName] = collection[key];
464-
if (keyName) locals[keyName] = key;
465-
return valueFn(scope, locals);
466-
};
467-
}
468-
469-
if (multiple) {
470-
if (keyName) locals[keyName] = key;
471-
calculateViewValue();
472-
return (selectAsFn || valueFn)(scope, locals);
473-
}
474-
475-
if (key == '?') {
476-
value = undefined;
477-
} else if (key === ''){
478-
value = null;
426+
function getViewValue(key, collection) {
427+
if (key === '?') {
428+
return undefined;
429+
} else if (key === '') {
430+
return null;
479431
} else {
480-
value = calculateViewValue();
432+
var viewValueFn = selectAsFn ? selectAsFn : valueFn;
433+
return callExpression(viewValueFn, key, collection);
481434
}
435+
}
482436

483-
return value;
437+
// TODO: Use this method everywhere
438+
function callExpression(exprFn, key, collection) {
439+
// TODO: keep this as a central object, no need to recreate this
440+
var locals = {};
441+
locals[valueName] = collection[key];
442+
if (keyName) locals[keyName] = key;
443+
return exprFn(scope, locals);
484444
}
485445

486446
function isSelected(viewValue, locals, selectedSet) {

0 commit comments

Comments
 (0)