Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow optionsValue to accept an array map #727

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 9 additions & 3 deletions build/output/knockout-latest.debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -2516,22 +2516,28 @@ ko.bindingHandlers['options'] = {

var option = document.createElement("option");

function applyToObject(object, predicate, defaultValue) {
function applyToObject(object, predicate, defaultValue, index) {
var predicateType = typeof predicate;
if (predicateType == "function") // Given a function; run it against the data value
return predicate(object);
else if (predicateType == "string") // Given a string; treat it as a property name on the data value
return object[predicate];
else if (predicateType == "object" && predicate != null) {
if (Object.prototype.toString.apply(predicate) === '[object Array]')
return predicate[index]; // Given an array: translate the value to the text
else
return predicate[object]; // Given an object: map the value to the text
}
else // Given no optionsText arg; use the data value itself
return defaultValue;
}

// Apply a value to the option element
var optionValue = applyToObject(arrayEntry, allBindings['optionsValue'], arrayEntry);
var optionValue = applyToObject(arrayEntry, allBindings['optionsValue'], arrayEntry, i);
ko.selectExtensions.writeValue(option, ko.utils.unwrapObservable(optionValue));

// Apply some text to the option element
var optionText = applyToObject(arrayEntry, allBindings['optionsText'], optionValue);
var optionText = applyToObject(arrayEntry, allBindings['optionsText'], optionValue, i);
ko.utils.setTextContent(option, optionText);

element.appendChild(option);
Expand Down