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

Supports for object data sources #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
20 changes: 18 additions & 2 deletions angular-selectize.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
var optionsFn = $parse(optionsExpression);
var displayFn = $parse(match[2] || match[1]);
var valueFn = $parse(match[2] ? match[1] : valueName);
var objectKey = match[5];

watchParentOptions();

Expand Down Expand Up @@ -207,6 +208,15 @@
}
}

function arrayObjectIndexOf(arr, obj) {
for (var i = 0; i < arr.length; i++) {
if (angular.equals(arr[i], obj)) {
return i;
}
};
return -1;
}

function getSelectedItems(model) {
model = angular.isArray(model) ? model : [model] || [];

Expand All @@ -222,8 +232,14 @@

var selections = options.reduce(function(selected, option, index) {
var optionValue = getOptionValue(option);
if (model.indexOf(optionValue) >= 0) {
selected[optionValue] = index;
if (angular.isObject(optionValue)) {
if (arrayObjectIndexOf(model, optionValue) >= 0) {
selected[optionValue[objectKey]] = index;
}
} else {
if (model.indexOf(optionValue) >= 0) {
selected[optionValue] = index;
}
}
return selected;
}, {});
Expand Down