Skip to content
This repository was archived by the owner on Sep 8, 2020. It is now read-only.

Unobtrusive support for ngOptions (fixes #46) #204

Open
wants to merge 4 commits 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
28 changes: 21 additions & 7 deletions src/select2.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
* @params [options] {object} The configuration options passed to $.fn.select2(). Refer to the documentation
*/
angular.module('ui.select2', []).value('uiSelect2Config', {}).directive('uiSelect2', ['uiSelect2Config', '$timeout', function (uiSelect2Config, $timeout) {

var NG_OPTIONS_REGEXP = /^\s*(.*?)(?:\s+as\s+(.*?))?(?:\s+group\s+by\s+(.*))?\s+for\s+(?:([\$\w][\$\w]*)|(?:\(\s*([\$\w][\$\w]*)\s*,\s*([\$\w][\$\w]*)\s*\)))\s+in\s+(.*?)(?:\s+track\s+by\s+(.*?))?$/;

var options = {};
if (uiSelect2Config) {
angular.extend(options, uiSelect2Config);
Expand All @@ -17,9 +20,14 @@ angular.module('ui.select2', []).value('uiSelect2Config', {}).directive('uiSelec
var watch,
repeatOption,
repeatAttr,
hasOptions = !!tAttrs.ngOptions,
isSelect = tElm.is('select'),
isMultiple = angular.isDefined(tAttrs.multiple);

if (hasOptions) {
hasOptions = tAttrs.ngOptions.match(NG_OPTIONS_REGEXP)[7];
}

// Enable watching of the options dataset if in use
if (tElm.is('select')) {
repeatOption = tElm.find( 'optgroup[ng-repeat], optgroup[data-ng-repeat], option[ng-repeat], option[data-ng-repeat]');
Expand Down Expand Up @@ -93,7 +101,7 @@ angular.module('ui.select2', []).value('uiSelect2Config', {}).directive('uiSelec
}, true);
controller.$render = function () {
if (isSelect) {
elm.select2('val', controller.$viewValue);
elm.select2('val', elm.val());
} else {
if (opts.multiple) {
var viewValue = controller.$viewValue;
Expand All @@ -108,7 +116,7 @@ angular.module('ui.select2', []).value('uiSelect2Config', {}).directive('uiSelec
} else if (!controller.$viewValue) {
elm.select2('data', null);
} else {
elm.select2('val', controller.$viewValue);
elm.select2('val', elm.val());
}
}
}
Expand All @@ -122,7 +130,7 @@ angular.module('ui.select2', []).value('uiSelect2Config', {}).directive('uiSelec
}
// Delayed so that the options have time to be rendered
$timeout(function () {
elm.select2('val', controller.$viewValue);
elm.select2('val', elm.val());
// Refresh angular to remove the superfluous option
elm.trigger('change');
if(newVal && !oldVal && controller.$setPristine) {
Expand All @@ -132,6 +140,12 @@ angular.module('ui.select2', []).value('uiSelect2Config', {}).directive('uiSelec
});
}

if (hasOptions) {
scope.$watchCollection(hasOptions, function(newVal, oldVal) {
elm.select2('val', elm.val());
});
}

// Update valid and dirty statuses
controller.$parsers.push(function (value) {
var div = elm.prev();
Expand All @@ -147,9 +161,9 @@ angular.module('ui.select2', []).value('uiSelect2Config', {}).directive('uiSelec

if (!isSelect) {
// Set the view and model value and update the angular template manually for the ajax/multiple select2.
elm.bind("change", function (e) {
elm.bind('change', function (e) {
e.stopImmediatePropagation();

if (scope.$$phase || scope.$root.$$phase) {
return;
}
Expand All @@ -176,8 +190,8 @@ angular.module('ui.select2', []).value('uiSelect2Config', {}).directive('uiSelec
}
}

elm.bind("$destroy", function() {
elm.select2("destroy");
elm.bind('$destroy', function() {
elm.select2('destroy');
});

attrs.$observe('disabled', function (value) {
Expand Down