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

Enable watching on nested ng-repeats. #177

Open
wants to merge 1 commit 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 9 additions & 14 deletions src/select2.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,17 @@ angular.module('ui.select2', []).value('uiSelect2Config', {}).directive('uiSelec
require: 'ngModel',
priority: 1,
compile: function (tElm, tAttrs) {
var watch,
repeatOption,
repeatAttr,
var watches = [],
isSelect = tElm.is('select'),
isMultiple = angular.isDefined(tAttrs.multiple);

// 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]');

if (repeatOption.length) {
repeatAttr = repeatOption.attr('ng-repeat') || repeatOption.attr('data-ng-repeat');
watch = jQuery.trim(repeatAttr.split('|')[0]).split(' ').pop();
}
watches = tElm.find('[ng-repeat], [data-ng-repeat]').map(function() {
var repeatOption = $(this);
var repeatAttr = repeatOption.attr('ng-repeat') || repeatOption.attr('data-ng-repeat');
return jQuery.trim(repeatAttr.split('|')[0]).split(' ').pop();
}).get();
}

return function (scope, elm, attrs, controller) {
Expand Down Expand Up @@ -115,7 +112,7 @@ angular.module('ui.select2', []).value('uiSelect2Config', {}).directive('uiSelec
};

// Watch the options dataset for changes
if (watch) {
angular.forEach(watches, function(watch) {
scope.$watch(watch, function (newVal, oldVal, scope) {
if (angular.equals(newVal, oldVal)) {
return;
Expand All @@ -130,7 +127,7 @@ angular.module('ui.select2', []).value('uiSelect2Config', {}).directive('uiSelec
}
});
});
}
});

// Update valid and dirty statuses
controller.$parsers.push(function (value) {
Expand All @@ -149,7 +146,7 @@ angular.module('ui.select2', []).value('uiSelect2Config', {}).directive('uiSelec
// Set the view and model value and update the angular template manually for the ajax/multiple select2.
elm.bind("change", function (e) {
e.stopImmediatePropagation();

if (scope.$$phase || scope.$root.$$phase) {
return;
}
Expand Down Expand Up @@ -199,8 +196,6 @@ angular.module('ui.select2', []).value('uiSelect2Config', {}).directive('uiSelec
$timeout(function () {
elm.select2(opts);

// Set initial value - I'm not sure about this but it seems to need to be there
elm.select2('data', controller.$modelValue);
// important!
controller.$render();

Expand Down
2 changes: 1 addition & 1 deletion test/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = function(config) {
// list of files / patterns to load in the browser
files: [
// Dependencies
'bower_components/jquery/jquery.js',
'bower_components/jquery/dist/jquery.js',
'bower_components/angular/angular.js',
'bower_components/angular-mocks/angular-mocks.js',
'bower_components/select2/select2.js',
Expand Down